Last active
December 13, 2015 23:39
-
-
Save lizzie/4992907 to your computer and use it in GitHub Desktop.
等价于ES5的bind()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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