【发布时间】:2011-10-20 13:55:26
【问题描述】:
以下 sn-p 显示了我是如何创建继承的。我受到this 文章的启发。
function Base() {
this.init();
}
Base.prototype.init = function () {
};
Derived1.prototype = new Base();
Derived1.prototype.constructor = Derived1;
Derived1.superclass = Base.prototype;
function Derived1() {
}
Derived1.prototype.init = function() {
Derived1.superclass.init.call(this);
};
Derived2.prototype = new Derived1();
Derived2.prototype.constructor = Derived2;
Derived2.superclass = Derived1.prototype;
function Derived2() {
Derived2.superclass.init.call(this);
}
当浏览器加载这个js文件时,所有的构造函数都会被调用。
Derived2.prototype = new Derived1();
这可能会导致一些意想不到的行为。
有没有办法阻止这种行为?
【问题讨论】:
-
您能否更具体地说明意外行为?
-
例如,如果您在构造函数中进行一些 DOM 操作。