Created
November 14, 2009 02:33
-
-
Save cowboy/234344 to your computer and use it in GitHub Desktop.
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
var obj = { | |
prop: 'foo', | |
method1: function(){ return this.prop; } | |
}; | |
obj.method2 = function(){ return this.method1(); }; | |
function bindify( obj, name, fn ) { | |
obj[ name ] = function(){ | |
return fn.apply( obj, arguments ); | |
}; | |
}; | |
bindify( obj, 'method3', function(){ return this.method1(); } ); | |
var method1 = obj.method1; | |
var method2 = obj.method2; | |
var method3 = obj.method3; | |
console.log( obj.prop ); // "foo" | |
console.log( obj.method1() ); // "foo" | |
console.log( obj.method2() ); // "foo" | |
console.log( obj.method3() ); // "foo" | |
console.log( method1() ); // undefined | |
console.log( method2() ); // undefined | |
console.log( method3() ); // "foo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment