简体   繁体   English

isPrototypeOf和__proto__的结果不同

[英]isPrototypeOf and __proto__ have different results

The following two expressions: 以下两个表达式:

"abc".__proto__.__proto__ === Object.prototype  // true
Object.prototype.isPrototypeOf("abc")           // false

The first expression proves that Object.prototype lies in the prototype chain of "abc". 第一个表达式证明Object.prototype位于“ abc”的原型链中。 However, the second expression gets an opposite result. 但是,第二个表达式得到相反的结果。

I am very confused. 我很困扰。 Hope anyone can explain. 希望任何人都能解释。

"abc" is not an object. "abc"不是对象。 When you evaluate "abc".__proto__ , a String wrapper object is implicitly constructed to retrieve the prototype of, and Object.prototype is in that wrapper object's prototype chain. 当您评估"abc".__proto__ ,将隐式构造一个String包装器对象以检索其原型,而Object.prototype在该包装器对象的原型链中。

Object.prototype.isPrototypeOf("abc") doesn't construct a wrapper object. Object.prototype.isPrototypeOf("abc")不会构造包装器对象。 It just looks at "abc" , sees that "abc" isn't an object and has no prototype chain, and returns false. 它仅查看"abc" ,看到"abc"不是对象,也没有原型链,并返回false。 You can see this in the ECMAScript spec (version 6): 您可以在ECMAScript规范 (版本6)中看到以下内容:

When the isPrototypeOf method is called with argument V, the following steps are taken: 使用参数V调用isPrototypeOf方法时,将执行以下步骤:

  1. If Type(V) is not Object, return false. 如果Type(V)不是Object,则返回false。

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

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