I'm new to JavaScript so if I'm misunderstanding a concept, please feel free to let me know.
I have the following class:
var Human = function () {};
Object.defineProperty(Human.prototype, 'name', {
get: function () { return this._name; },
set: function (value) { this._name = value; },
configurable: true,
enumerable: true
});
I then define the following child object:
var Man = function () {};
Man.prototype = new Human(); //Am I inherting Human's prototype (and properties) here?
Man.prototype.name = 'Matt'; //If so, I should be setting Man's name property (overriding the value inherited from Human)
However, console.log(Man.name)
prints out ""
.
Why is this and how can I correctly override Human
's property?
PS:
I have also tried
Man.name = 'Matt';
instead of
Man.prototype.Name = 'Matt';
But I got the same behaviour.
Ps2:
I should also note that if I execute console.log(Man.prototype._name)
i get the expected output "Matt"