【发布时间】:2011-01-06 07:03:15
【问题描述】:
您好,我是 Javascript OO 的新手,想了解有关继承的更多信息。希望大家多多指教!
我看到了这篇很棒的帖子: How to "properly" create a custom object in 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;
上面的帖子声称,为了在继承时不调用“man”的构造函数,可以使用这样的帮助器:
function subclassOf(base) {
_subclassOf.prototype= base.prototype;
return new _subclassOf();
}
function _subclassOf() {};
shawn.prototype = subclassOf(man);
虽然我理解它的意图,但我不明白为什么我们不能打电话
shawn.prototype = man.prototype;
我看到它的工作原理完全相同。还是我缺少什么?提前致谢!
【问题讨论】:
标签: javascript oop