使用'instanceof function(){}'的原因? [英] Reason behind using 'instanceof function() {}'?

查看:175
本文介绍了使用'instanceof function(){}'的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mozilla开发人员中心,有一个关于 Function.prototype.bind 功能,并为不支持此功能的浏览器提供兼容性功能。



然而, ,当分析这个兼容性代码时,我找不到为什么他们使用 instanceof nop nop 已被设置为 function(){} 。 ECMA规范中的 bind 的哪一部分对应于?变量是什么变量是 function(){}



下面的函数返回,所以我不完全知道它用于什么。当执行 instanceof函数(){} 检查时,返回true是什么?

 (function(){})instanceof(function(){})// false 

代码如下:

  Function.prototype.bind = function(obj){
if(typeof this!= ='function')
抛出新的TypeError('Function.prototype.bind - 试图绑定的内容不可调用');

var slice = [] .slice,
args = slice.call(arguments,1),
self = this,
nop = function(){} ,
bound = function(){
return self.apply(this instanceof nop?this:(obj || {}),
args.concat(slice.call(arguments))) ;
};

bound.prototype = this.prototype;

返回界限;
};


解决方案

有人编辑了一些有用的部分。以下是它的样子:

  Function.prototype.bind = function(obj){
var slice = [] .slice,
args = slice.call(arguments,1),
self = this,
nop = function(){},
ound = function(){
return self.apply(this instanceof nop?this:(obj || {}),
args.concat(slice.call(arguments)));
};

//这些行是重要部分
nop.prototype = self.prototype;
bound.prototype = new nop();

返回界限;
};

我回答了另一个问题,它在问这个问题时是相同的(但代码是正确的):<一个href =https://stackoverflow.com/questions/5774070/mozillas-bind-function-question/5774147#5774147> mozilla的绑定功能问题。



这个这个nop 检查实例的原因是,如果你将绑定函数作为构造函数调用(即使用 new operator),这个绑定到新对象,而不是传递给 bind nop 基本上被插入到原型链中,以便当你调用函数时作为构造函数,这个 是一个 nop 的实例。



因此,如果运行 var bound = original.bind(someObject); ,原型链将如下所示:

 
原始
|
nop
|
bound

我为什么用 nop 来代替这个instanceof self 是为了让绑定函数拥有自己的 prototype 属性(它继承自的)。这可能不是因为这可能是它被部分编辑出来的原因。无论如何,现在的代码是不正确的,但只要您不使用该函数作为构造函数就会工作。


On Mozilla Developer Center, there is a page about the Function.prototype.bind function and provides a compatibility function for browsers which do not support this function.

However, when analyzing this compatibility code I cannot find out why they use instanceof nop. nop has been set to function() {}. What part of the ECMA specification on bind does this correspond with? And what variables are an instance of function() {}?

The following returns false, so I don't completely know what it is used for. What things return true when doing an instanceof function() {} check?

(function() {}) instanceof (function() {}) // false

The code is as follows:

Function.prototype.bind = function( obj ) {
    if(typeof this !== 'function')
      throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');

    var slice = [].slice,
        args = slice.call(arguments, 1), 
        self = this, 
        nop = function () {}, 
        bound = function () {
          return self.apply( this instanceof nop ? this : ( obj || {} ), 
                              args.concat( slice.call(arguments) ) );    
        };

    bound.prototype = this.prototype;

    return bound;
};

解决方案

Someone edited out the part that makes it useful. Here's what it used to look like:

Function.prototype.bind = function( obj ) {
    var slice = [].slice,
    args = slice.call(arguments, 1), 
    self = this, 
    nop = function () {}, 
    bound = function () {
        return self.apply( this instanceof nop ? this : ( obj || {} ), 
                            args.concat( slice.call(arguments) ) );    
    };

    // These lines are the important part
    nop.prototype = self.prototype;
    bound.prototype = new nop();

    return bound;
};

I answered another question that was asking the same thing (but when the code was correct) here: mozilla's bind function question.

The reason for the this instanceof nop check is so that if you call the bound function as a constructor (i.e. with the new operator), this is bound to the new object instead of whatever you passed to bind.

To explain the "important part", nop is basically getting inserted into the prototype chain so that when you call the function as a constructor, this is an instance of nop.

So if you run var bound = original.bind(someObject); the prototype chain will look like this:

  original
     |
    nop
     |
   bound

My guess for why they used nop instead of this instanceof self is so that the bound function would have it's own prototype property (that inherits from self's). It's possible that it's not supposed to which could be why it got partially edited out. Regardless, the code as it is now is not correct, but will work as long as you don't use the function as a constructor.

这篇关于使用'instanceof function(){}'的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆