So, basically, I think this boils down to a fundamental misunderstanding of what a prototype
is. Individual instances of an object do not have a .prototype
, rather, they have an internal link to the prototype of the constructor from which the object instance was created. This was formerly known as .__proto__
(AKA dunder-proto), but has since been officially deprecated.
More recently, to reference the prototype of the constructor for an object instance, you can access a property called .constructor
. (*Note*: .constructor
may be undefined depending on how the object was created). From this, you can access the .prototype
.
Similarly, you can use Object.getPrototypeOf(obj)
and Object.setPrototypeOf(obj)
where obj
is an instance of an object.
For example:
var x = Object.create(null);
console.log("x.prototype", x.prototype);
var y = Object.create({a: "foo"});
console.log("y.prototype:", y.prototype);
The .prototype
is undefined in both cases, because object instances do not have a prototype property, only object constructors do.
That being said, we can access the prototype that an object instance was created from by using the .constructor.prototype
property, like so:
function myObj (){
this.a = "foo";
}
// myObj is a constructor function, which has a prototype that we can set properties on
myObj.prototype.b = "bar";
// obj is an instance of an object, which does not have a prototype property
var obj = new myObj();
console.log("obj.prototype:", obj.prototype);
console.log("Object.getPrototypeOf(obj):", Object.getPrototypeOf(obj));
console.log("obj.constructor.prototype:", obj.constructor.prototype);