Skip to content

Instantly share code, notes, and snippets.

@ValeriiVasin
Created January 17, 2012 05:45
Show Gist options
  • Select an option

  • Save ValeriiVasin/1625039 to your computer and use it in GitHub Desktop.

Select an option

Save ValeriiVasin/1625039 to your computer and use it in GitHub Desktop.
[javascript patterns] Function binding
if ( !Function.prototype.bind ) {
Function.prototype.bind = function( obj ) {
var slice = Array.prototype.slice,
args = slice.call(arguments, 1),
self = this,
nop = function () {},
bound = function () {
return self.apply( this instanceof nop ? this : ( obj || {} ),
args.concat( slice.call(arguments) ) );
};
nop.prototype = self.prototype;
bound.prototype = new nop();
return bound;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment