【发布时间】:2012-11-24 13:44:48
【问题描述】:
我很难理解在构建层次结构时将 javascript 对象的“构造函数”属性设置为子类的必要性。我发现下面的代码在不更改构造函数属性的情况下完成了预期的操作,但在我发现的关于该主题的几乎所有参考资料中,构造函数都是明确设置的。我错过了什么吗? (我在 ECMAScript 规范中也没有发现任何明确使用它)。
A = function() {
this.value = "a";
this.A = function() {
window.alert( this.value + " instanceof A : " + ( this instanceof A ) );
}
}
B = function() {
this.value = "b";
this.B = function() {
window.alert( this.value + " instanceof B : " + ( this instanceof B ) );
}
}
B.prototype = new A();
test = function() {
var b = new B();
b.A();
b.B();
}
【问题讨论】:
标签: javascript inheritance properties constructor