嗨,我是Javascript OO的新手,想要了解有关继承的更多信息.希望你能提供一些建议!
我看到这篇精彩文章: 如何在JavaScript中"正确"创建自定义对象?
正如我在其他网站上看到的那样谈论一个类是如何继承的,例如:
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;
Run Code Online (Sandbox Code Playgroud)
上面的帖子声称,为了在继承时不调用"man"的构造函数,可以使用这样的帮助器:
function subclassOf(base) {
_subclassOf.prototype= base.prototype;
return new _subclassOf();
}
function _subclassOf() {};
shawn.prototype = subclassOf(man);
Run Code Online (Sandbox Code Playgroud)
虽然我理解它的意图,但我不明白为什么我们不能打电话
shawn.prototype = man.prototype;
Run Code Online (Sandbox Code Playgroud)
我觉得它的作用完全一样.还是有什么我想念的?提前致谢!