gpt4 book ai didi

javascript - "this instanceof fNOP ? this"在做什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:56 25 4
gpt4 key购买 nike

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)));

如果thisfNOP 的实例,第一个参数将是this。如果不是,那么它将是 oThis 如果它是“真实的”(不是 null,不是 undefined 和任何与 false 同义的东西),或者 oThiswindow > 是错误的。

它是这段代码的简写:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com