Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a JavaScript namespace into multiple files

Let's say I have a namespace like that:

var myNamespace = {     foo: function() {     },     bar: function() {     } }; 

What is the best way to split this code into files defining foo and bar separately?

I'm not worried about loading time - I'll concatenate it back into one file before deployment.

like image 532
Piotr Zurek Avatar asked Mar 01 '11 03:03

Piotr Zurek


2 Answers

At the start of each file:

if(myNameSpace === undefined) {   var myNameSpace = {}; } 

File 1:

myNamespace.foo = function()... 

File 2:

myNamespace.bar = function()... 
like image 153
generalhenry Avatar answered Sep 28 '22 18:09

generalhenry


// File1: // top level namespace here: var myNamespace = myNamespace || {};  // File2: myNamespace.foo = function() {     // some code here... } 
like image 44
Mark Avatar answered Sep 28 '22 16:09

Mark