Resolved it, it turns native functions needs to be called from within window context so I ended binding the functions in window and doing double prototyping like what follows:
function define(w){ // for testing I use requirejs define instead
window.Test123=w;
}
(function(){
function WBase(){
for(var i in window){
if(typeof window[i]=='function'){
this[i]=window[i].bind(window);
}
}
}
WBase.prototype=window;
function W(){
var w=this;
Object.defineProperty(this,'window',{configurable:true,enumerable:true,writable:false,value:w});
}
W.prototype=new WBase();
define(function(){
var windowAlias=new W();
return (function(window){
var ret={}; // returns only the variables added to the alias window object
with(window){
// CODE FROM MODULE HERE WILL HAVE an alias to "window" wich means if it adds variables to window (global scope) it will be prevented and instead added to the alias window
for(var i in window){
if(window.hasOwnProperty(i)&&i!='window')
ret[i]=window[i];
}
}
return ret;
}).call(windowAlias,windowAlias)
});
})();
Test123(); // for testing
try pasting jquery in place of comment (// CODE FROM MODULE HERE...), you'll get an object with properties jQuery and $ and they will not be added to the global scope. and I ended up creating a page that appends these lines dynamically for javascript files requested through querystring
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…