Skip to content

Instantly share code, notes, and snippets.

@getify
Last active March 30, 2016 06:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/5254459 to your computer and use it in GitHub Desktop.
Save getify/5254459 to your computer and use it in GitHub Desktop.
a rather inefficient, but workable, "polyfill" (of sorts) for `currentThis` as proposed in: https://gist.github.com/getify/5253319
// a sort-of-polyfill for `currentThis` as proposed in: https://gist.github.com/getify/5253319
function lookupFn(oThis,fn) {
var ptr = oThis, i, keys;
while (ptr) {
keys = Object.keys(ptr);
for (i=0; i<keys.length; i++) {
if (ptr[keys[i]] === fn) {
return ptr;
}
}
ptr = Object.getPrototypeOf(ptr);
}
return oThis;
}
var Foo = Object.create(null);
Foo.me = "Foo";
Foo.identify = function() {
var currentThis = lookupFn(this,arguments.callee);
// whenever Foo#identify() is called, `currentThis` will
// always be `Foo`, but `this` will continue to follow the
// established rules for determining a `this` binding.
console.log("Me: " + this.me + "; Current: " + currentThis.me);
};
var Bar = Object.create(Foo);
Bar.me = "Bar";
Bar.another = function() {
var currentThis = lookupFn(this,arguments.callee);
console.log("Current: " + currentThis.me);
}
var bar1 = Object.create(Bar);
bar1.me = "bar1";
var bar2 = Object.create(Bar);
bar2.me = "bar2";
Foo.identify(); // "Me: Foo; Current: Foo"
Bar.identify(); // "Me: Bar; Current: Foo"
Bar.another(); // "Current: Bar"
bar1.identify(); // "Me: bar1; Current: Foo"
bar2.identify(); // "Me: bar2; Current: Foo"
bar1.another(); // "Current: Bar"
bar2.another(); // "Current: Bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment