欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

javascript原型链继承用法实例分析

时间:2021-05-21 08:26:21|栏目:JavaScript代码|点击:

本文实例分析了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;

改写:

复制代码 代码如下:
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; 
}

再改写(引用传递而不是值传递):

复制代码 代码如下:
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; 
}

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

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

复制代码 代码如下:
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; 
}

虽然提高了效率,但是这样的方法有个副作用,因为是引用传递,而不是值传递,所以“父对象”中的name值受到了影响。

子对象和父对象指向的是同一个对象。所以一旦子对象对齐原型进行修改,父对象也会随即被改变。

希望本文所述对大家的javascript程序设计有所帮助。

上一篇:Eval and new funciton not the same thing

栏    目:JavaScript代码

下一篇:BootStrap框架个人总结(bootstrap框架、导航条、下拉菜单、轮播广告carousel、栅格系统布局、标签页tabs、模态框、菜单定位)

本文标题:javascript原型链继承用法实例分析

本文地址:http://www.codeinn.net/misctech/125839.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有