if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
这是从bind MDC中挑选的,我不明白什么是 this instanceof fNOP ?这
正在做。有人可以教我吗?我想知道为什么不使用 oThis ||窗口
直接。
最佳答案
return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));
如果this
是fNOP
的实例,第一个参数将是this
。如果不是,那么它将是 oThis
如果它是“真实的”(不是 null,不是 undefined 和任何与 false 同义的东西),或者 oThis
的 window
> 是错误的。
它是这段代码的简写:
if(this instanceof fNOP){
firstparameter = this;
} else {
if(oThis){
firstParameter = oThis;
} else {
firstParameter = window;
}
}
关于javascript - "this instanceof fNOP ? this"在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10138998/