Ursprünglich war __proto__
ein Mozilla-Only-Hack in frühen Versionen von Mozilla-Browsern (Firefox vor 3.5). Es wurde in der ECMAScript 3.1 nur im Jahr 2008 kodifiziert.
Ein Unterschied ist, dass __proto__
geändert werden kann (eine schlechte Design-Praxis obwohl), während getPrototypeOf
eine Lesefunktion ist.
var parentObject = {"parentKey" : "parentValue"};
var childObject = {"childKey" : "childValue"};
childObject.__proto__ = parentObject;
console.log(childObject.parentKey); // parentValue
Object.getPrototypeOf(childObject) = {"parentKey" : "newValue"};
// ReferenceError: Invalid left-hand side in assignment
Ein weiterer Unterschied ist, wie sie den unwahrscheinlichen Fall eines Namenskonflikt behandeln:
var myObject = { __proto__ : "conflicts", getPrototypeOf : function() {return "does not conflict"} };
console.log(myObject.getPrototypeOf()); // "does not conflict"
// all objects inherit from Object.prototype, not Object, so there is no conflict
console.log(Object.getPrototypeOf(myObject)) // Object {}
console.log(myObject.__proto__); // Object {}, the value "conflicts" is lost
Danke für den Hinweis auf den Namen Konflikt, gut zu wissen. – simonzack