简体   繁体   English

为什么我的instanceof运算符不响应true?

[英]Why my instanceof operator doesn't respond true?

I'm trying out instanceof operator. 我正在尝试instanceof运算符。 I tried out something like this. 我尝试了这样的事情。

function f(){ return f; }
new f() instanceof f;
// false

Why this came out to be false , when these are true 当这些是true时,为什么这出来是false

function f(){ return f; }
new f() instanceof Function; 
// true

function f(){ return f; }
new f() instanceof Object;
//true

When tried to save this to a variable still resulted same 当尝试将其保存到变量时仍然导致相同

function f(){ return f; }
var n = new f();

n instanceof f;
// false

n();
// function f()

n() instanceof f;
// false

n instanceof Function     // true
n() instanceof Function   // true

Why return f; 为什么return f; statement have changed everything? 声明改变了一切吗? What did return f did to cause this behavior? return f导致了此行为的原因是什么?

First, I'd recommend checking out the article on the new operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new 首先,我建议您查看有关新运算符的文章: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

Specifically, note that 具体来说,请注意

When the code new Foo(...) is executed, the following things happen: 当执行代码new Foo(...)时,将发生以下情况:

  1. A new object is created, inheriting from Foo.prototype. 创建一个新对象,该对象继承自Foo.prototype。
  2. The constructor function Foo is called with the specified arguments, and with this bound to the newly created object. 使用指定的参数调用构造函数Foo,并将其绑定到新创建的对象。 new Foo is equivalent to new Foo(), ie if no argument list is specified, Foo is called without arguments. new Foo等效于new Foo(),即,如果未指定参数列表,则在不带参数的情况下调用Foo。
  3. The object returned by the constructor function becomes the result of the whole new expression. 构造函数返回的对象成为整个新表达式的结果。 If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. 如果构造函数未明确返回对象,则使用在步骤1中创建的对象。 (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.) (通常,构造函数不返回值,但是如果他们想覆盖常规的对象创建过程,则可以选择这样做。)

By explicitly returning f , your are overriding the normal creation process. 通过显式返回f ,您将覆盖正常的创建过程。 When you use instanceof , you are asking "Is n and instance of f". 当使用instanceof ,您会问“是n和f的实例”。 It's not. 不是。 It is f. f。 It is not an instance of itself. 它不是自身的实例。

Since clearly f is a function, and n === f , both will return true when you try to determine if they are functions. 由于显然f是一个函数,并且n === f ,当您尝试确定它们是否是函数时,两者都将返回true。 Additionally, in Javascript, functions themselves are objects (as are arrays), which is why new f() instanceof Object is true. 此外,在Javascript中,函数本身就是对象(与数组一样),这就是为什么new f() instanceof Object为true的原因。

You are returning f from your constructor. 您正在从构造函数返回f f is the constructor. f 构造函数。 So your constructor is never constructing an instance , you're just always testing whether the constructor is an instance of itself. 因此,构造函数从不构造实例 ,而只是在测试构造函数是否为其自身的实例。 Which it isn't, by definition. 根据定义,它不是。

By returning f , you return the function, which is no instance of f , because it lacks the call with new . 通过返回f ,您返回的函数不是f实例,因为它缺少对new的调用。

 function f() { return f; } console.log(new f() === f); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM