suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Javascript – Fragen zur Vererbung

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

<code>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"

</code>

1. Warum heißt es, dass das Namensattribut weder ein Attribut der TwoDShape()-Instanz noch ihres Prototypobjekts ist, Unterobjekte aber dennoch durch Vererbung auf dieses Attribut zugreifen können?

PHP中文网PHP中文网2845 Tage vor629

Antworte allen(2)Ich werde antworten

  • 滿天的星座

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

    因为子对象继承的是一整条原型链,而不是单个原型

    Antwort
    0
  • 伊谢尔伦

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

    挂载在原型链上的属性当然不是

    Antwort
    0
  • StornierenAntwort