Javascript继承扩展超过2次

时间:2013-11-26 09:51:55

标签: javascript inheritance multiple-inheritance

我想用Javascript继承做一些例子,我很惊讶得到超出最大调用堆栈大小

我正在扩展一个基类超过1次,然后抛出一个错误。 应该可以将基类扩展一次以上,对吧?

--> Class A
 --> Class B extends A
  --> Class C extends B

您可以在jsFiddle http://jsfiddle.net/gPYDd/3/中查看,也可以浏览以下代码。

function View () {
   this.__id = null;    
}

View.prototype.init = function (id) {
   this.__id = id; 
}

View.prototype.render = function() {
   this.__element = document.getElementById(this.__id);
   this.__originalContent = this.__element.innerHTML;
   this.__element.innerHTML = 'Do some basic stuff';
}

View.prototype.destroy = function () {
    this.__element.innerHTML = this.__originalContent;   
}

function ExtendedView () {  
    View.call(this);
}

ExtendedView.prototype = Object.create(View.prototype);
ExtendedView.prototype.constructor = ExtendedView;
ExtendedView.prototype.parent = View.prototype;

ExtendedView.prototype.render = function () {
    this.parent.render.call(this);
    this.__originalColor = this.__element.style.color;
    this.__element.style.color = 'red';   
}

ExtendedView.prototype.destroy = function () {
    this.parent.destroy.call(this);
    this.__element.style.color = this.__originalColor;   
}


function SuperExtendedView () {  
    ExtendedView.call(this);
}

SuperExtendedView.prototype = Object.create(ExtendedView.prototype);
SuperExtendedView.prototype.constructor = SuperExtendedView;
SuperExtendedView.prototype.parent = ExtendedView.prototype;

SuperExtendedView.prototype.render = function () {
    this.parent.render.call(this);
    this.__originalFontSize = this.__element.style.fontSize;
    this.__element.style.fontSize = '3em';   
}

SuperExtendedView.prototype.destroy = function () {
    this.parent.destroy.call(this);
    this.__element.style.fontSize = this.__originalFontSize;   
}

0 个答案:

没有答案