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

标签 javascript

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/

相关文章:

javascript - AngularJS 的 $routeProvider 错误

javascript - 如何在我的案例中显示重复元素

javascript - IndexOf 和多数组

javascript - 轻松纺车

javascript - 为什么要同时设置className和class?

javascript - $.cache 什么时候被认为太大了?

javascript - Bootstrap 3 Navbar 不会切换(已检查现有解决方案,单击时仍不会切换折叠)

javascript - 404 仅在第一张图像的 ionic /Angular 中的 ng-repeat 上找不到图像

javascript - 计算页面上的 javascript 函数数量(使用 javascript)

javascript - 数组上的 .map() 迭代器函数传递了什么?