Inheritance using prototype / "new"
Posted
by
mikkol
on Stack Overflow
See other posts from Stack Overflow
or by mikkol
Published on 2011-01-06T07:03:15Z
Indexed on
2011/01/06
9:53 UTC
Read the original article
Hit count: 503
JavaScript
|oop
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!
© Stack Overflow or respective owner