__Proto__ vs Prototype In JavaScript
Note that since let and const are supported in all modern browsers and a good practice I will be using them instead of var. To support legacy environments, just use var instead.
prototype refers to a set of properties and functions that custom objects will have that inherit from the object that it is on. It can be thought of as a blueprint for child objects. For example:
console.log('prototype example:'); const ObjectA = function() {}; ObjectA.prototype.property1 = 'value1'; // Inherit (The constructor was not updated but is unused here) const ObjectAChild = function() {}; ObjectAChild.prototype = Object.create(ObjectA.prototype); ObjectAChild.prototype.property2 = 'value2'; let objectA = new ObjectA(); console.log(objectA.property1); // value 1 let objectAChild = new ObjectAChild(); console.log(objectAChild.property1, objectAChild.property2); // value 1 value 2 ObjectA.prototype.property1 = 'another value'; console.log(objectA.property1, objectAChild.property1); // another value another value
Notice how properties can be updated and instantly reflect in children. This is because if a property is not found at a current object, it checks its prototype (what the parent said should be inherited). Then if not found, it checks the prototypes prototype and so on until the Object prototype is reached. The implication is that inherited objects are just objects linked in a special way to another object.
The value of these links is the standard that makes accessing and analyzing them easier and the built in prototype checking algorithm. One of the ways to analyze an object is to look at an objects internal prototype. Remember that an objects prototype is for objects inherited from it, not the objects internal prototype. __proto__ allows you to get this. This can be used to get the properties that the project has that were not inherited. Using the previous code:
console.log(objectAChild.__proto__); // { property2: 'value2' }
Of course, you could just use the prototype directly on parent object that the child object was created from if you have access to it:
console.log(ObjectAChild.prototype); // { property2: 'value2' }
Finally, __proto__ has great JS environment support and is part of the ECMAScript standard (Which JavaScript is based on). But it was only added as a temporary measure until better APIs are fully supported like Object.getPrototypeOf(). So it is best to use Object.getPrototypeOf() when possible.
Github Location: https://github.com/Jacob-Friesen/obscurejs/blob/master/2017/protoVsPrototype.js









