Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created November 14, 2009 02:33
Show Gist options
  • Save cowboy/234344 to your computer and use it in GitHub Desktop.
Save cowboy/234344 to your computer and use it in GitHub Desktop.
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