Skip to content

Instantly share code, notes, and snippets.

@brigand
Forked from qcom/bind.js
Last active January 2, 2016 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brigand/8279099 to your computer and use it in GitHub Desktop.
Save brigand/8279099 to your computer and use it in GitHub Desktop.
Function.prototype.bind = function(thisObj) {
var that = this, args = Array.prototype.slice.call(arguments, 1);
return function() {
var xArgs = Array.prototype.slice.call(arguments);
return that.apply(thisObj, args.concat(xArgs));
};
};
function fn() {
return this;
}
var a = { a : 1 };
var b = { b : 2 };
console.log(fn.call(a)); // { a : 1 }
console.log(fn.call(b)); // { b : 1 }
var boundFn = fn.bind(a);
console.log(fn(a)); // global object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment