I'm trying to do the following:
function SomeFunctionConstructor() {
this.someMainVariable = "someValue";
this.manipulatePrototype = () => {
this.someProtoVariable = "some new value";
}
}
SomeFunctionConstructor.prototype.someProtoVariable = "Some proto value";
var someInstance = new SomeFunctionConstructor();
Now, if I console.log(someInstance)
, it prints:
{
someMainVariable: 'someValue',
manipulatePrototype: [Function]
}
And now, when I do someInstance.manipulatePrototype()
and then console.log(someInstance)
, it prints:
{
someMainVariable: 'someValue',
manipulatePrototype: [Function],
someProtoVariable: 'some new value'
}
Why does it create a new someProtoVariable
directly on the instance instead of updating the someProtoVariable
on the prototype? :(
i think you are confusing between an instance and the prototype . and instance cant over the object prototype by design. if this would happens then every instance will be able to destroy the prototype chain and effect all other instances that are built from this prototype
if you print someInstance.someProtoVariable without calling manipulatePrototype then you will get the old value.
and after you will get the new value .
this.someProtoVariable did override the default value for the instance but didn`t override the prototype itself
you can read more about prototype here Inheritance_and_the_prototype_chain