this series as effective JavaScript 's reading notes.
in the ES5 introduced in the object.getprototypeof as a standard for getting object prototype objects API . However, in very many operating environments. also provides a special __proto__ attribute to achieve the same purpose.
since not all environments offer this __proto__ property, and each environment is implemented differently, some results may be inconsistent:
In some environments var is empty = Object.create (null); Object with no prototype "__proto__" in empty; False (in some environments)//var empty = Object.create (null) in some environments; Object with no prototype "__proto__" in empty; True (in some environments)
So when the environment supports object.getprototypeof method, use it preferentially.
Even if not supported. can also be implemented in order to achieve a:
if (typeof object.getprototypeof = = = "undefined") {object.getprototypeof = function (obj) {var t = typeof obj;if (!obj | | ( T!== "Object" && t!== "function") {throw new TypeError ("Not a object");} return obj.__proto__;};}
The code above first checks the current environment, assuming it has supported the object.getprototypeof , it will not be defined again and again.
In addition, when using __proto__ will cause some errors in the Item will be discussed in the.
Summarize:
- preferential use of standard methods object.getprototypeof . Instead of the nonstandard __proto__ property.
- for non- ES5 environment to achieve a object.getprototypeof method. This preserves the consistency of the code.
Effective JavaScript Item 31 takes precedence over object.getprototypeof instead of __proto__