I was examining various JavaScript libraries for learning purposes. Basically I want to find the best way to initiliaze a namespace and see how pros load up all the associated files in their libraries. I came across this a few times in the main file (for example lets call it myNameSpace.js) that the library user would call in a few libraries:
(function() {
var jsFiles = window.MyNameSpace;
window.MyNameSpace = {
_getScriptLocation: (function() {
/* some code here */
})
};
if(!jsFiles) {
jsFiles = [/* An array of ALL the library files! */];
}
for(var i=0, len = jsFiles.length; i<len; i++) {
scriptTags[i] = "<script src='" + jsFiles[i] + "'></script>";
}
if(scriptTags.length > 0) {
document.write(scriptTags.join(""));
}
})();
So, with this setup, if the library user wanted to include only certain portions of the library, they would specify before loading the myNameSpace.js by doing something along the lines of:
<script type='text/javascript'> window.MyNameSpace = ["libraryFile1.js", "libraryFile2.js", "libraryFile3.js"]</script>
<script type='text/javascript' src="MyNameSpace.js"></script>
My question is, window.MyNameSpace is an object and if the script assigns jsFiles to an array setup as window.MyNameSpace, this is going to be an assignment by reference, correct? But immediately after that line, window.MyNameSpace gets completely changed. So shouldn't jsFiles no longer reference the original array that was passed in? What am I missing here in my understanding?