search

Home  >  Q&A  >  body text

javascript - Questions about inheritance

function extend(Child,Parent){
var F=function(){};
F.prototype=Parent.prototype;
Child.prototype=new F();
Child.prototype.constructor=Child;
Child.uber=Parent.prototype;
}

function Shape(){}
Shape.prototype.name='Shape';
Shape.prototype.toString=function(){
return this.constructor.uber
    ?this.constructor.uber.toString()+', ' + this.name
    :this.name;
};

function TwoDShape(){}
extend(TwoDShape,Shape);
TwoDShape.prototype.name='2D shape';

function Triangle(side,height){
this.side=side;
this.height=height;
}

var F=function(){};
extend(Triangle,TwoDShape);
Triangle.prototype.name='Triangle';
Triangle.prototype.getArea=function(){
return this.side*this.height/2;
};

var Shape=function(){};
var TwoDShape=function(){};
Shape.prototype.name='shape';
Shape.prototype.toString=function(){
return this.uber
?this.uber.toString()+', ' + this.name
:this.name;
};

extend(TwoDShape,Shape);    
var td=new TwoDShape();

td.name;//"shape"

1. Why is it said that the name attribute will neither be an attribute of the TwoDShape() instance nor its prototype object, but sub-objects can still access this attribute through inheritance?

PHP中文网PHP中文网2723 days ago580

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-06-15 09:23:31

    Because the child object inherits an entire prototype chain, not a single prototype

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-15 09:23:31

    The properties mounted on the prototype chain are certainly not

    reply
    0
  • Cancelreply