Created
August 7, 2015 10:43
-
-
Save shentao/b8960da1e56f4995fdaf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var isFunction = function(o) { | |
return typeof o === 'function'; | |
}; | |
var bind, | |
slice = [].slice, | |
proto = Function.prototype, | |
featureMap; | |
featureMap = { | |
'function-bind': 'bind' | |
}; | |
function has(feature) { | |
var prop = featureMap[feature]; | |
return isFunction(proto[prop]); | |
} | |
// check for missing features | |
if (!has('function-bind')) { | |
// adapted from Mozilla Developer Network example at | |
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind | |
bind = function bind(obj) { | |
var 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 = this.prototype || {}; // Firefox cries sometimes if prototype is undefined | |
bound.prototype = new nop(); | |
return bound; | |
}; | |
proto.bind = bind; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment