Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created January 18, 2010 04:39
Show Gist options
  • Select an option

  • Save subtleGradient/279786 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/279786 to your computer and use it in GitHub Desktop.
Dynamic Namespace Localization
function evalWith(namespaces, code){
if ({}.toString.call(namespaces) != '[object Array]')
namespaces = [namespaces];
var imports = {
require: function(){},
exports: {},
module: {}
};
namespaces.push(imports);
var args = [];
var values = [];
for (var property in namespace){
args.push(property);
values.push(namespace[property]);
}
args.push(code);
Function.apply(this, args).apply(imports.exports, values);
return imports.exports;
};
var namespace = {
Thing: function(){ this.rocks = true; }
}
file = "exports.MyThing = new Thing();";
console.log(
evalWith(namespace, file)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment