【笔记】javascript原型链继承实例

function Shape(){
 this.name = 'shape';
 this.toString = function(){
  return this.name;
 }
}

function TwoDShape(){
 this.name = '2D shape';
}
function Triangle(side,height){
 this.name = 'Triangle';
 this.side = side;
 this.height = height;
 this.getArea = function(){
  return this.side*this.height/2;
 };
}

/* inheritance */
TwoDShape.prototype = new Shape();
Triangle.prototype = new TwoDShape();


当我们对对象的prototype属性进行完全重写时,有时候会对对象constructor属性产生一定的负面影响。
所以,在我们完成相关的继承关系设定后,对这些对象的const属性进行相应的重置是一个非常好的习惯。

TwoDShape.prototype.constructor = TwoDShape;
Triangle.prototype.constructor = Triangle;


改写:

<span style="font-family:Microsoft YaHei;font-size:14px;color:#333333;">function Shape(){}

Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
 return this.name;
}

function TwoDShape(){}

TwoDShape.prototype = new Shape();
TwoDShape.prototype.constructor = TwoDShape;

TwoDShape.prototype.name = '2d shape';

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

Triangle.prototype = new TwoDShape;
Triangle.prototype.constructor = Triangle;

Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
 return this.side*this.height/2;
}</span>
再改写(引用传递而不是值传递):
<span style="font-size:14px;color:#333333;">function Shape(){}

Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
 return this.name;
}

function TwoDShape(){}

TwoDShape.prototype = Shape.prototype;
TwoDShape.prototype.constructor = TwoDShape;

TwoDShape.prototype.name = '2d shape';

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

Triangle.prototype = TwoDShape.prototype;
Triangle.prototype.constructor = Triangle;

Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
 return this.side*this.height/2;
}</span>



虽然提高了效率,但是这样的方法有个副作用,因为是引用传递,而不是值传递,所以“父对象”中的name值受到了影响。
子对象和父对象指向的是同一个对象。所以一旦子对象对齐原型进行修改,父对象也会随即被改变。


再再改写(使用临时构造器):

<span style="font-size:14px;color:#333333;">function Shape(){}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
 return this.name;
}
function TwoDShape(){}
var F = function(){}
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.prototype.name = '2d shape';
function Triangle(side,height){
 this.side = side;
 this.height = height;
}
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
 return this.side*this.height/2;
}</span>



虽然提高了效率,但是这样的方法有个副作用,因为是引用传递,而不是值传递,所以“父对象”中的name值受到了影响。
子对象和父对象指向的是同一个对象。所以一旦子对象对齐原型进行修改,父对象也会随即被改变。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值