简体   繁体   English

为什么{} __ proto__ instanceof!== Object

[英]why {}__proto__ instanceof !== Object

This just does not make sense to me, please someone, explain the last line of it: 这对我来说没有意义,请有人解释它的最后一行:

var o = {};
o.__proto__.isPrototypeOf(Object)  // true
o.__proto__.constructor.name       // 'Object'
o.__proto__.toString()             // '[object Object]'
o.__proto__ instanceof Object      // false (wtf???)

Let me rephrase my original question - I am not looking for understanding HOW IT WORKS INTERNALLY BUT WHY IT WORKS THIS WAY (although sometimes it helps) - I mean : why JS engine constructs dunder proto ( __proto__ ) AS an object which is not instance of Object EVEN if it has 'constructor' set AND even if ALL object literals ARE instances of Object (eg. o instanceof Object === true , ({}) instanceof Object === true ). 让我重新解释一下我原来的问题 - 我不是在寻找理解它如何在内部工作但是为什么它以这种方式工作(虽然有时它有所帮助) - 我的意思是:为什么JS引擎构造dunder proto( __proto__ proto __proto__ )AS一个不是实例的对象对象即使它有'构造函数'设置,即使所有对象文字都是对象的实例(例如, o instanceof Object === true({}) instanceof Object === true )。

In other words - instanceof of WHAT is dunder proto when dunder proto is not manually constructed ? 换句话说 - 当dunder proto不是手动构建时, WHAT的实例是dunder proto?

And why it has 'constructor' property set if "it's not an instance" as somebody wrote? 为什么它有“构造函数”属性设置,如果“它不是一个实例”,就像有人写的那样? According to MDN : "All objects inherit a constructor property from their prototype" - if dunder proto object has constructor property it should mean it was inherited from 'some' prototype... So, it would make sense to me if 'constructor' property was NOT SET for 'root' dunder proto 根据MDN:“所有对象都从其原型继承构造函数属性” - 如果dunder proto对象具有构造函数属性,那么它应该意味着它是从'some'原型继承的......所以,如果'constructor'属性对我有意义没有为'root'dunder proto设置

(I did not use dunder proto as it was discouraged but yesterday I read it's official part of ES6 so I took a look at it) (我没有使用dunder proto,因为它被劝阻但昨天我读了ES6的官方部分,所以我看了一下)

instanceof does a bit of book-keeping, and then executes the HasInstance "method". instanceof做了一些簿记,然后执行HasInstance “方法”。

HasInstance is defined as: HasInstance定义为:

Returns a Boolean value indicating whether the argument is likely an Object that was constructed by this object. 返回一个布尔值,指示参数是否可能是此对象构造的Object。

Since your object's prototype wasn't constructed, it must always return false when using instanceof . 由于构造对象的原型,因此在使用instanceof时必须始终返回false。 In contrast, 相反,

o instanceof Object

returns true, because o is an instance. 返回true,因为o 一个实例。

How does this actually work? 这实际上是如何工作的? Well, HasInstance goes through the whole prototype chain of the object it is inspecting. 好吧, HasInstance遍历它正在检查的对象的整个prototype链。 But o.__proto__ doesn't have a prototype - so it's not considered an object, and HasInstance returns false. o.__proto__ 没有原型 - 因此它不被视为对象, HasInstance返回false。

If you're used to dealing with class-based OOP languages, Javascript's OOP(/FP :P) can be quite a challenge. 如果你习惯于处理基于类的OOP语言,那么Javascript的OOP(/ FP:P)可能是一个很大的挑战。 I'd refer you to some article that explains this well, but sadly, I haven't found any so far - and reading the specification is likely to make you even more confused (it's not one of the better specifications out there). 我会向你推荐一些解释得很好的文章,但遗憾的是,到目前为止我还没有找到 - 并且阅读规范可能会让你更加困惑(这不是那里更好的规范之一)。

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

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