I have three script files. main.js, script1.js, script2.js.
The main.js just include that scripts into the document:
function IncludeJs(sFileName) {
document.writeln('<script type="text/javascript" src="' + sFileName + '"></script>');
}
IncludeJs("../script1.js");
IncludeJs("../script2.js");
The script1.js and script2.js code is under same namespace.
script1.js:
var sharedNamespace = sharedNamespace || {};
(function () {
"use strict";
function func1(){
....
}
}).apply(sharedNamespace );
script2.js:
var sharedNamespace = sharedNamespace || {};
(function () {
"use strict";
function func2(){
return func1();
}
}).apply(sharedNamespace );
The func2 is not working because func1 is undefined. How i can put script1.js and script2.js under same scope with shared variables?
The solution of sharedNamespace.func1 = function(){}
, is worst for me, because i don't want to expose this function to client who use my library...