简体   繁体   中英

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:

newObj.__proto__ === f.prototype .

And newObj.__proto__ should be set automatically on new f() call.

Constructor functions implicitly return this . this is an instance of the constructor function and so, if you return this the function will evaluate to true.

You do not return this . You return f which is not an instance of the constructor, thus the expression returns false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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