【笔记】javascript原型链继承实例
2015-01-14 21:27
155 查看
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值受到了影响。
子对象和父对象指向的是同一个对象。所以一旦子对象对齐原型进行修改,父对象也会随即被改变。
相关文章推荐
- javascript原型链继承用法实例分析
- Javascript学习笔记2 继承 原型链
- Javascript 笔记(4)----继承与原型链
- javascript原型链继承用法实例分析
- Javascript学习笔记7——原型链的原理
- Javascript学习笔记9——prototype封装继承
- JavaScript 框架开发笔记 [三] 自定义HTML对像的扩展与继承
- javascript学习笔记 (五) -继承和Closures
- [导入]JavaScript基础之继承(附实例)
- javascript面向对象学习笔记(一)——继承
- JavaScript 的原型继承与类式继承学习笔记
- JavaScript基础之继承(附实例) [ZT]
- javascript学习笔记(九)javascript中的原型(prototype)及原型链的继承方式
- Ext学习笔记02 - 构造方法,类继承,类实例方法重写
- EXT 笔记 构造方法,类继承,类实例方法重写
- Javascript学习笔记7 原型链的原理
- JavaScript笔记 - 对象继承的几种方式
- JavaScript基础之继承(附实例)
- Javascript 继承机制实例
- javascript 学习笔记(5)---继承