Created
January 18, 2010 04:39
-
-
Save subtleGradient/279786 to your computer and use it in GitHub Desktop.
Dynamic Namespace Localization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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