Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created March 5, 2011 06:42
Show Gist options
  • Select an option

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

Select an option

Save subtleGradient/856186 to your computer and use it in GitHub Desktop.
Globalize JS
var Foo = 'bar';
this.Foo = 'bar';
var global = function(){return this || (1,eval)('this')}();
global.Foo = 'bar';
!function(){
var GLOBAL = function(){return this || (1,eval)('this')}()
Function.prototype.globalize = function(){
return this.apply(GLOBAL, arguments)
}
}()
!function(){
this.Foo = 'bar';
}.globalize()
function Export(fn){var G = this||(7,eval)('this'); fn.call(G,G)}
Export (function(exports){
exports.Foo = 'bar';
})
function Export(fn){fn.call(this||(7,eval)('this'))}
Export (function(){
this.Foo = 'bar';
})
@cpojer

cpojer commented Mar 5, 2011

Copy link
Copy Markdown

the upside of applyToGlobal is that you only have to get the context once and reuse that on every function call.

@ibolmo

ibolmo commented Mar 5, 2011

Copy link
Copy Markdown

Like I had mentioned on another comment.. is this possible as well?

var global = this || (1, eval)('this');
global.global = global;

(function(){
    this....
}).call(global);

// or I guess directly:

global.Function.prototype.... 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment