简体   繁体   English

怪异行为的Javascript实例

[英]Javascript instanceof weird behavior

Why does it returns false? 为什么返回假?

function f(){ return f; }
new f() instanceof f; // Prints false instead of true 

As I understand, in this particular case instanceof should check something like: 据我了解,在这种特殊情况下, instanceof应该检查如下内容:

newObj.__proto__ === f.prototype . newObj.__proto__ === f.prototype

And newObj.__proto__ should be set automatically on new f() call. 并且newObj.__proto__应该在new f()调用上自动设置。

Constructor functions implicitly return this . 构造函数隐式返回this this is an instance of the constructor function and so, if you return this the function will evaluate to true. this 构造函数的一个实例,因此,如果返回this函数,则该函数的值为true。

You do not return this . 退还this You return f which is not an instance of the constructor, thus the expression returns false . 您返回的f不是构造函数的实例,因此表达式返回false

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

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