I am new to Javascript and have a question about using inheritance with it
This article has been helpful so far (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#More_flexible_constructors), but the behaviour I am observing in Chrome's debugger does not seem to be matching what I'd expect to see.
I have two cases:
Base = function () {
this.value = 0;
};
Base.prototype.constructor = Base;
Derived = function () {
Base.call(this);
};
Derived.prototype = new Base();
Derived.prototype.constructor = Derived;
In the debugger I see this:
After stepping over the assignment, I see this
The value in the instance has changed but the value in the prototype has not. Is this to be expected?
The second approach I do not quite understand is this (referenced here again - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#More_flexible_constructors (see the code snipped at the bottom of section 'Property Inheritance Revisited')
Base = function () {
};
Base.prototype.constructor = Base;
Base.prototype.value = 0;
// Derived as before...
and after the assignment
Value has been added as a new property. Shouldn't this have changed the value in the prototype? Or do I explicitly need to access the value by going through the prototype - e.g. derived.value.prototype.value = 5
Any information on this would be greatly appreciated!
Thanks!
UPDATE:
Thank you to everyone who responded, it turns out it was to do with my not using Object.create at the appropriate time. I updated my code to this:
and in the debugger I got what I expected:
This looks right! :)
Thank you to @sixfingeredman and @Bergi for your help!