Skip to content

Instantly share code, notes, and snippets.

@zmaril
Created June 13, 2012 00:50
Show Gist options
  • Save zmaril/2921087 to your computer and use it in GitHub Desktop.
Save zmaril/2921087 to your computer and use it in GitHub Desktop.
Global Bindings
//With statement doesn't do what I hoped, which was overriding the whole smear of things.
//Actually, just not possible with closures at all I don't think. Once a value has been bound, there isn't much you can do.
Function.prototype.bind_global = function bind_global(ctx) {
return (function(t,args){
return function() {
var g = [].slice.call(args,1,2);
g = g ? g : window;
with(g){
return t.apply(ctx, [].slice.call(args, 2));
}
};
})(this,arguments);
};
var x = {n : 1};
var y = {k : 1};
var n = 2;
var f = function(n){
return n+this.n;
};
console.log("f");
console.log("undefined",f(undefined));
console.log("null",f(null));
console.log("10",f(10));
console.log("");
var g = f.bind_global(x,null,10);
console.log("g");
console.log("undefined",g(undefined));
console.log("null",g(null));
console.log("10",g(10));
console.log("");
var h = f.bind_global(x,y,10);
console.log("h");
console.log("undefined",h(undefined));
console.log("null",h(null));
console.log("10",h(10));
console.log("");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment