《javaScript面向对象编程指南》第二版P182--怎么理解Shape.prototype.toString=function(){ var const = this.constructor; return const.uber? this.const.uber.toString()+","+this.name:this.name;}源码是:function Shape(){}Shape.prototype.name="shape";Shape.prototype.toString=function(){ var const = this.constructor; return const.uber? this.const.uber.toString()+","+this.name:this.name;}function TwoDShape(){}var F=function(){};F.prototype=Shape.prototype;TwoDShape.prototype=new F();TwoDShape.prototype.constructor=TwoDShape;TwoDShape.uber=Shape.prototype;TwoDShape.prototype.name="2D shape";function Triangle(side,height) { this.side=side; this.height=height;}var F=function(){};F.prototype=TwoDShape.prototype;Triangle.prototype=new F();Triangle.prototype.constructor=Triangle;Triangle.uber=TwoDShape.prototype;Triangle.prototype.name="Triangle";Triangle.prototype.getArea=function(){ return this.side*this.height/2;};var my=new Triangle(5,10);my.toString();
1 回答
橋本奈奈未
TA贡献436条经验 获得超108个赞
这个涉及到JS的原型链,比较复杂,百度一下吧,其实这部分我认为了解即可,不用原型链一样可以把JS玩的很溜,知道大概怎么回事就行,然后如果要使用继承可以使用Object.create()方法,关联性相比原型链简单的不能再简单。另外说句题外话,这本书我没看过,不过,这个例子是有问题的,const是关键字,用来作为变量名会出错的
添加回答
举报
0/150
提交
取消