Inheritance using prototype / "new"
- by mikkol
Hi I'm new in Javascript OO and want to know more about about inheritance. Hope you can provide some advice!
I see this great post:
How to "properly" create a custom object in JavaScript?
which talks about how a class is inherited as I see in other websites, ex.:
function man(x) {
this.x = x;
this.y = 2;
}
man.prototype.name = "man";
man.prototype.two = function() {
this.y = "two";
}
function shawn() {
man.apply(this, arguments);
};
shawn.prototype = new man;
The above post claims that in order not to call "man"'s constructor while inheriting, one can use a helper like this instead:
function subclassOf(base) {
_subclassOf.prototype= base.prototype;
return new _subclassOf();
}
function _subclassOf() {};
shawn.prototype = subclassOf(man);
While I understand its intention, I don't see why we can't call
shawn.prototype = man.prototype;
I see it works exactly the same. Or is there something I'm missing? Thanks in advance!