Skip to content

Instantly share code, notes, and snippets.

@mahendrakalkura
Created February 11, 2017 18:28
Show Gist options
  • Save mahendrakalkura/06e478f6d9c199a4f2b2805057094247 to your computer and use it in GitHub Desktop.
Save mahendrakalkura/06e478f6d9c199a4f2b2805057094247 to your computer and use it in GitHub Desktop.
Rewrite the bind method from scratch.
var bind = function(context) {
var bindee = this;
var parameters = Array.prototype.slice.call(arguments, 1);
parameters = parameters.concat(Array.prototype.slice.call(arguments));
var noop = function() {};
var method = function() {
return bindee.apply(this instanceof noop? this: context, parameters);
};
if (this.prototype) {
noop.prototype = this.prototype;
}
method.prototype = new noop();
return method;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment