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

前端之家收集整理的这篇文章主要介绍了javascript原型链继承用法实例分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_404_0@本文实例分析了javascript原型链继承的用法分享给大家供大家参考。具体分析如下:


@H_404_0@<div class="codetitle"><a style="CURSOR: pointer" data="60056" class="copybut" id="copybut60056" onclick="doCopy('code60056')"> 代码如下:
<div class="codebody" id="code60056">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();


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


@H_404_0@<div class="codetitle"><a style="CURSOR: pointer" data="51770" class="copybut" id="copybut51770" onclick="doCopy('code51770')"> 代码如下:
<div class="codebody" id="code51770">TwoDShape.prototype.constructor = TwoDShape;
Triangle.prototype.constructor = Triangle;


@H_404_0@改写:
<div class="codetitle"><a style="CURSOR: pointer" data="29744" class="copybut" id="copybut29744" onclick="doCopy('code29744')"> 代码如下:
<div class="codebody" id="code29744">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;
}


@H_404_0@再改写(引用传递而不是值传递):
<div class="codetitle"><a style="CURSOR: pointer" data="69265" class="copybut" id="copybut69265" onclick="doCopy('code69265')"> 代码如下:
<div class="codebody" id="code69265">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.sidethis.height/2;
}


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


@H_404_0@再再改写(使用临时构造器):
<div class="codetitle"><a style="CURSOR: pointer" data="50702" class="copybut" id="copybut50702" onclick="doCopy('code50702')"> 代码如下:<div class="codebody" id="code50702">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;
}


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


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


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

原文链接:https://www.f2er.com/js/56062.html
javascript原型链用法用法继承继承

猜你在找的JavaScript相关文章