Instantly share code, notes, and snippets.

Embed
What would you like to do?
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';
})
@subtleGradient

This comment has been minimized.

Copy link
Owner Author

subtleGradient commented Mar 5, 2011

The advantage of Export or globalize is that you could implement some sort of fancy AMD system using that same API or something maybe I guess sortof.

@cpojer

This comment has been minimized.

Copy link

cpojer commented Mar 5, 2011

Note that Export won't work globally because we'd have to define it in every file. The idea of applyToGlobal is to not have to fetch the global object mor than once.

Also, I liked how you replaced 1 with 7 in the indirect eval call, heh.

@subtleGradient

This comment has been minimized.

Copy link
Owner Author

subtleGradient commented Mar 5, 2011

I think we can handle 53 characters at the top of each file ;)

@cpojer

This comment has been minimized.

Copy link

cpojer commented Mar 5, 2011

yeah, but potentially use eval in every single module? I dislike.

@subtleGradient

This comment has been minimized.

Copy link
Owner Author

subtleGradient commented Mar 5, 2011

What's the alternative?
You have to get Export or applyGlobal into the universe somehow.

@cpojer

This comment has been minimized.

Copy link

cpojer commented Mar 5, 2011

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

@ibolmo

This comment has been minimized.

Copy link

ibolmo commented Mar 5, 2011

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