色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

javascript 繼承實(shí)現(xiàn)方法

下面我給出幾種常用的方法:
1 .對(duì)象冒充
原理: 構(gòu)造函數(shù)使用this關(guān)鍵字給所有屬性和方法賦值, 因?yàn)闃?gòu)造函數(shù)只是一個(gè)函數(shù),所以可以使ClassA的構(gòu)造函數(shù)成為classB的方法,然后調(diào)用它.這樣classB就會(huì)收到classA的構(gòu)造函數(shù)中定義的屬性和方法.例子:
復(fù)制代碼 代碼如下:
function classA(name)
{
this.name=name;
this.showName=function(){alert(this.name);}
}
function classB(name)
{
this.newMethod = classA;
this.newMethod(name);
}
obj = new classA("hero");
objB = new classB("dby");
obj.showName(); // print hero
objB.showName(); // print dby 說(shuō)明classB 繼承了classA的方法.

對(duì)象冒充可以實(shí)現(xiàn)多重繼承 例如
復(fù)制代碼 代碼如下:
function classz(){
this.newMethod = classX;
this.newMethod();
delete this.newMethod;
this.newMethod=classY;
this.newMethod():
delete this.newMethod;
}

但是如果classX和classY有相同的屬性或者方法,classY具有高優(yōu)先級(jí).
2.call()方法
call方法使與經(jīng)典的對(duì)象冒充法就相近的方法,它的第一個(gè)參數(shù)用作this的對(duì)象,其他參數(shù)都直接傳遞給函數(shù)自身.
復(fù)制代碼 代碼如下:
function sayName(perfix)
{
alert(perfix+this.name);
}
obj= new Object();
obj.name="hero";
sayName.call(obj,"hello," );
function classA(name)
{
this.name=name;
this.showName=function(){alert(this.name);};
}
function classB(name)
{
classA.call(this,name);
}
objB = new classB("bing");
objB.showName();////說(shuō)明classB繼承classA的showName方法

3.apply()方法
aplly()方法有2個(gè)參數(shù),一個(gè)用作this對(duì)象,一個(gè)使傳遞給函數(shù)的參數(shù)數(shù)組.
復(fù)制代碼 代碼如下:
function sayName(perfix)
{
alert(perfix+this.name);
}
obj= new Object();
obj.name="hero";
sayName.aplly(obj,new Array("hello,") );

4. 原型鏈
prototype對(duì)象的任何屬性和方法都會(huì)被傳遞給對(duì)應(yīng)類(lèi)的所有實(shí)例,原型鏈就是用這種方式來(lái)顯現(xiàn)繼承.
復(fù)制代碼 代碼如下:
function classA (){}
classA.prototype.name="hero";
classA.prototype.showName=function(){alert(this.name)}
function classB(){}
classB.prototype=new classA();
objb = new classB()
objb.showName();//print hero 說(shuō)明b繼承了a的方法

這里需要注意 調(diào)用classA的構(gòu)造函數(shù)時(shí),沒(méi)有給它傳遞參數(shù),這是原型鏈的標(biāo)準(zhǔn)做法,確保函數(shù)的構(gòu)造函數(shù)沒(méi)有任何參數(shù).
并且 子類(lèi)的所有屬性和方法,必須出現(xiàn)在prototype屬性被賦值后,應(yīng)為在它之前賦的值會(huì)被刪除.因?yàn)閷?duì)象的prototype屬性被替換成了新對(duì)象,添加了新方法的原始對(duì)象將被銷(xiāo)毀.

5 混和方式
就是用冒充方式 定義構(gòu)造函數(shù)屬性,用原型法定義對(duì)象方法.
復(fù)制代碼 代碼如下:
function classA(name)
{
this.name=name;
}
classA.prototype.showName=function(){alert(this.name)}
function classB(name)
{
classA.call(this,name);
}
classB.prototype = new classA();
classB.prototype.showName1=function(){alert(this.name+"*****");};
obj = new classB("hero");
obj.showName();
obj.showName1();

在classB的構(gòu)造函數(shù)中通過(guò)調(diào)用call方法 繼承classA中的name屬性,用原型鏈來(lái)繼承classA的showName方法.

JavaScript技術(shù)javascript 繼承實(shí)現(xiàn)方法,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 山东| 阿鲁科尔沁旗| 孟州市| 白沙| 永仁县| 镇江市| 巩义市| 武鸣县| 革吉县| 潼南县| 湄潭县| 城口县| 通辽市| 通江县| 通化县| 宁乡县| 涞水县| 苍山县| 美姑县| 五常市| 拜泉县| 荣昌县| 新安县| 和田市| 德令哈市| 石台县| 万州区| 得荣县| 宝应县| 介休市| 谢通门县| 察雅县| 光山县| 洛阳市| 南宫市| 乌拉特前旗| 天长市| 宁津县| 利川市| 西城区| 星座|