Last active
August 29, 2015 14:08
-
-
Save andsve/03ef7c2f911d3b061c55 to your computer and use it in GitHub Desktop.
Faking a JavaScript "local" context + exporting predefined specific functions.
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 FakeContext( src ) { | |
return (Function( '"use strict";' + | |
"if (this != window) {" + | |
" for (var __i in this) {" + | |
" eval( 'var ' + __i + ' = this[ __i ];' );" + | |
" }" + | |
"}" + | |
src + | |
"\n" + | |
"if (typeof(initialize) != 'undefined') { this.initialize = initialize; };" + | |
"if (typeof(shutdown) != 'undefined') { this.shutdown = shutdown; };" + | |
"return this;" | |
) | |
); | |
} | |
var a = FakeContext("console.log('This is a name:', name); function initialize() { console.log('init'); }"); | |
var b = a.apply( { name: "John Doe" } ); | |
b.initialize(); // Running the "exported" function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment