亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript關(guān)于繼承的用法匯總

 更新時間:2014年12月20日 15:06:35   投稿:shichen2014  
這篇文章主要介紹了javascript關(guān)于繼承的用法,實(shí)例匯總了常見的javascript關(guān)于繼承的用法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例匯總了javascript關(guān)于繼承的用法。分享給大家供大家參考。具體如下:

例子:

復(fù)制代碼 代碼如下:
/**
* 實(shí)現(xiàn)子類繼承父類,但不會產(chǎn)生多余的屬性和方法
* @returns {Function}
*/
define(function(){
return function(subType, superType){
var proto = new Object(superType.prototype);
proto.constructor = subType;
subType.prototype = proto;
};
});
//——————————————————————————
define(function(){
function ostring(s)
{
this.str = s;
this.length = this.str.length;
}
ostring.prototype.show = function(){
alert(this.str);
};
return ostring;
});
//——————————————————————————
define(['inherit', 'ostring'], function(inherit, ostring){
function wstring(s){
//用call實(shí)現(xiàn)調(diào)用父類構(gòu)造函數(shù)
ostring.call(this, s);
this.chlength = 2 * s.length;
}
//繼承其他的屬性
inherit(wstring, ostring);
wstring.prototype.add = function(w)
{
alert(this.str + w);
};
return wstring;
});

再看例子
一、用function實(shí)現(xiàn):

復(fù)制代碼 代碼如下:
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function() {
    return this.name;
}
function Author(name, books) {
    this.inherit=person;
    this.inherit(name);
    this.books = books;
   
}
var au=new Author("dororo","Learn much");
au.name

或者同等效力的:
復(fù)制代碼 代碼如下:
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function() {
    return this.name;
}
function Author(name, books) {
    Person.call(this, name);
    this.books = books;
   
}
var au=new Author("dororo","Learn much");
au.getName

由于這只是將this作為參數(shù),調(diào)用父類Person的構(gòu)造函數(shù),把賦予父類的所有域賦予Author子類,所以任何父類Person構(gòu)造函數(shù)之外的定義的域(原型prototype),子類都不會繼承。所以上面例子中,au.getName將是沒有被定義的(undefined),因?yàn)間etName是在Person的原型對象中定義的。

而且,子類的構(gòu)造函數(shù)要在定義自己的域之前調(diào)用父類構(gòu)造函數(shù),免得子類的定義被父類覆蓋掉。也就是說,Author定義屬性book要在Person.call之后,否則會被Person中屬性覆蓋。同時,在子類中也最好不要用prototype來定義子類的函數(shù)域,因?yàn)樵谝粋€子類被new,實(shí)例化之后就要執(zhí)行prototype,然后才是調(diào)用父類的構(gòu)造函數(shù),這樣也容易被父類的屬性覆蓋掉。

二、用prototype實(shí)現(xiàn):

復(fù)制代碼 代碼如下:
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function() {
    return this.name;
}
function Author(name, books) {
    this.books = books; 
}
Author.prototype=new Person(name);
Author.prototype.constructor=Author;
Author.prototype.getBooks = function() {
    return this.books;
}
var au1=new Author("dororo1","Learn much");
var au2=new Author("dororo2","Learn less");
alert(au1.getName());
alert(au2.getName());

這種方法避免了function實(shí)現(xiàn)中,無法繼承prototype的問題。因?yàn)?Author.prototype=new Person(name);new Person()實(shí)例會調(diào)用Person構(gòu)造和原型的所有屬性。但是缺點(diǎn)是已經(jīng)實(shí)例化了Author.prototype。所以當(dāng)子類實(shí)例化的時候,所有非基本數(shù)據(jù)類型都是reference copy。所以上面例子中,無論實(shí)例au1,還是au2返回的值都是dororo1.

三、用“混合”實(shí)現(xiàn)

復(fù)制代碼 代碼如下:
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function() {
    return this.name;
}
function Author(name, books) {
    this.base = new Person(name);
    for(var key in this.base){
        if(!this[key]){
           this[key]=this.base[key];
           }
           }
    this.book=books;
}
var au1=new Author("dororo1","work");
var au2=new Author("dororo2","play");
alert(au1.getName());
alert(au2.getName());
au1.book;
au2.book;

 
屬于擴(kuò)展,把父類的所有域都拷貝到子類。完全沒有上述兩方面的問題。
寄生組合模式)

JS的繼承包括屬性的繼承和方法的繼承,他們分別通過不同的方法來實(shí)現(xiàn)。
1.屬性的繼承

屬性的繼承通過改變函數(shù)的執(zhí)行環(huán)境來實(shí)現(xiàn)的。而改變函數(shù)的執(zhí)行環(huán)境可以使用call()和apply()兩種方法來實(shí)現(xiàn)。

我們首先創(chuàng)建一個Animal“類”(因?yàn)镴S中沒有類的概念,這里只是一個模擬,它實(shí)際上只是一個Function函數(shù)對象)。

復(fù)制代碼 代碼如下:
function Animal(typeName) {
//為當(dāng)前方法的執(zhí)行環(huán)境(this)添加一個屬性typeName
//但是執(zhí)行環(huán)境(this)要執(zhí)行這個函數(shù)的時候才能確定
this.typeName = typeName;
this.colors = ["red","while"];
}
//想函數(shù)的原型里 添加 兩個(對象共享的)的方法
Animal.prototype.Shout = function () { alert("我是:--" + this.typeName);};
Animal.prototype.Eat = function () { alert("我是:--" + this.typeName) };
//--定義一個獅子--“類”(其實(shí)就是一個函數(shù))
function Lion(tn) {
//--執(zhí)行Animal方法,并通過apply的第一個參數(shù) 修改了Animal的執(zhí)行環(huán)境為Lion的this
//同樣的,Lion的this,也要在執(zhí)行的時候才能確定是誰
Animal.apply(this,["獅子"]);//--繼承了父類的變量屬性,this因?yàn)槭莕ew了Lion,this是Lion
}
Lion.prototype = Animal.prototype; //繼承父類的方法,搞定--但是這寫不好,當(dāng)子類再添加方法時候,父類同樣也有此方法,這是指針引用
Lion.prototype.Hunt = function () {
alert("我是:獅子,我要去捕獵~~·~");
}
var aminm = new Animal();
aminm.Hunt(); //---可以訪問到子類的方法,這樣就不好了
//----那么如何解決這個問題呢》??????
//---解決方案:繼承方法時候可以這樣寫:
Lion.prototype = new Animal();//繼承父類的方法,把Animal對象賦給了prototype原型,其實(shí)它里面也有屬性
var lion = new Lion(); //new 關(guān)鍵字除了創(chuàng)建的,還會修改Lion對象的執(zhí)行環(huán)境為Lion對象本身
// ---換句話說,就是new完了之后,Lion函數(shù)里的this就是Lion函數(shù)本身了,然后調(diào)用Lion函數(shù)

分析一下new關(guān)鍵字:

而new關(guān)鍵字是十分偉大的,在上段代碼中,new關(guān)鍵字完成了以下幾項(xiàng)工作:
1)開辟堆空間,以準(zhǔn)備存儲Lion對象
2)修改Lion對象本身的執(zhí)行環(huán)境,使得Lion函數(shù)的this指向了Lion函數(shù)對象本身。
3)調(diào)用Lion“類”的“構(gòu)造函數(shù)”,創(chuàng)建Lion對象
4)將Lion函數(shù)對象的堆地址賦值給變量l,這個時候l就指向了這個Lion函數(shù)對象
lion.Shout();
lion.Eat();
但是這種繼承有個缺點(diǎn):就是父類的構(gòu)造函數(shù)的被調(diào)用了兩次,call一次,然后new又一次。

希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論