Skip to content

Instantly share code, notes, and snippets.

@lizzie
Last active December 13, 2015 23:39
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 lizzie/4992907 to your computer and use it in GitHub Desktop.
Save lizzie/4992907 to your computer and use it in GitHub Desktop.
等价于ES5的bind()
if (!Function.prototype.bind) {
Function.prototype.bind = function (obj) {
var slice = [].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;
}
}
// from JavaScript Web Applications by MacCaw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment