Skip to content

Instantly share code, notes, and snippets.

@kgarfinkel
Last active December 26, 2015 00:29
Show Gist options
  • Save kgarfinkel/7064111 to your computer and use it in GitHub Desktop.
Save kgarfinkel/7064111 to your computer and use it in GitHub Desktop.
Javascript's 'bind' re-implemented.
var bind = function(fn, context) {
var args = Array.prototype.slice.call(arguments, 2);
return function() {
return fn.apply(context, args.concat(Array.prototype.slice.call(arguments)));
};
};
@imcodetolive
Copy link

var args = [].slice.call(arguments, 2); // Line 2 error, Array object can be overwritten

@Gistavo
Copy link

Gistavo commented Oct 23, 2013

Line 2: better Apply an Array :)

@jjenzz
Copy link

jjenzz commented Mar 26, 2014

Mine :)

Function.prototype.bind = function (scope) {
  var args = [].slice.call(arguments, 1),
      fn = this;

  return function () {
    fn.apply(scope, args.concat([].slice.call(arguments)));
  };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment