Friday 27 December 2019

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.


Answer




At the start of each file:



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


File 1:



myNamespace.foo = function()...



File 2:



myNamespace.bar = function()...

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...