3.一个绑定函数也能使用new操作符创建对象:这种行为就像把原函数当成构造器。提供的 this 值被忽略,同时调用时的参数被提供给模拟函数。
// Yes, it does work with `new funcA.bind(thisArg, args)` if (!Function.prototype.bind) (function(){ var ArrayPrototypeSlice = Array.prototype.slice; Function.prototype.bind = function(otherThis) { if (typeofthis !== 'function') { // closest thing possible to the ECMAScript 5 // internal IsCallable function thrownewTypeError('Function.prototype.bind- what is trying to be bound is not callable'); }
var baseArgs= ArrayPrototypeSlice .call(arguments, 1), baseArgsLength = baseArgs.length, fToBind = this, fNOP = function() {}, fBound = function() { // reset to default base arguments baseArgs.length = baseArgsLength; baseArgs.push.apply(baseArgs, arguments); // 当作为构造函数时,this 指向实例,此时结果为 true, 将绑定函数的 this 指向该实例,可以让实例获得来自绑定函数的值 return fToBind.apply( fNOP.prototype.isPrototypeOf(this) ? this : otherThis, baseArgs ); }; if (this.prototype) { // Function.prototype doesn't have a prototype property fNOP.prototype = this.prototype; } fBound.prototype = new fNOP(); return fBound; }; })();