简体   繁体   中英

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". However, the second expression gets an opposite result.

I am very confused. Hope anyone can explain.

"abc" is not an object. 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.

Object.prototype.isPrototypeOf("abc") doesn't construct a wrapper object. It just looks at "abc" , sees that "abc" isn't an object and has no prototype chain, and returns false. You can see this in the ECMAScript spec (version 6):

When the isPrototypeOf method is called with argument V, the following steps are taken:

  1. If Type(V) is not Object, return 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