国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

理解javascript對象繼承

2019-11-20 10:15:12
字體:
供稿:網(wǎng)友

先從一個(gè)問題進(jìn)行研究深入,什么是javascript對象繼承?

比如我們有一個(gè)“動(dòng)物”對象的構(gòu)造函數(shù)。

  function animal() {    this.type = '動(dòng)物';  }

還有一個(gè)“貓”對象的構(gòu)造函數(shù)。

  function cat(name,color) {    this.name = name;    this.color = color;  }

我們知道貓也屬于動(dòng)物,如果這個(gè)貓對象想要繼承動(dòng)物對象的屬性,我們該怎么做呢?

構(gòu)造函數(shù)綁定
使用構(gòu)造函數(shù)綁定是最簡單的方法,使用call或者apply將父對象綁定在自對象上就可以了。

 function cat(name,color) {    animal.apply(this,arguments);    this.name = name;    this.color = color;  }  var cat1 = new cat("haha", 'red');  console.log(cat1.type); //動(dòng)物

不過這種方法比較少見。

拷貝繼承
如果把父對象的所有屬性和方法,拷貝進(jìn)子對象,也可以實(shí)現(xiàn)繼承。

  function extend(Child, Parent) {    var p = Parent.prototype;    var c = Child.prototype;    for (var i in p) {      c[i] = p[i];      }    c.uber = p;   //橋梁作用  }

使用方法:

 extend(cat, animal);  var cat1 = new cat("haha","red");  alert(cat1.type);   // 動(dòng)物

原型繼承(prototype)
相比于上面的直接綁定,原型繼承的方法比較常見,對于prototype,我自己簡單總結(jié)了一下。

每個(gè)函數(shù)都有一個(gè)prototype屬性,這個(gè)屬性是指向一個(gè)對象的引用,當(dāng)使用new關(guān)鍵字創(chuàng)建新實(shí)例的時(shí)候,這個(gè)實(shí)例對象會(huì)從原型對象上繼承屬性和方法。

也就是說,如果將“貓”構(gòu)造函數(shù)的prototype屬性指向一個(gè)“動(dòng)物”實(shí)例,那么再創(chuàng)建“貓”對象實(shí)例的時(shí)候,就繼承了“動(dòng)物”對象的屬性和方法了。

繼承實(shí)例

 cat.prototype = new animal();  cat.prototype.constructor = cat;  var cat1 = new cat("haha","red");  console.log(cat1.constructor == cat);  //true  console.log(cat1.type); // 動(dòng)物

1、代碼第一行,我們將cat函數(shù)的prototype對象指向一個(gè)animal對象的實(shí)例(其中就包含了animal的type屬性了)。

2、代碼第二行是什么意思呢?

1)、首先,假如我們沒有加這行代碼,運(yùn)行

    cat.prototype = new animal();
    console.log(cat.prototype.constructor == animal);  //true
也就是說,其實(shí)每個(gè)prototype對象都有一個(gè)constructor屬性,指向它的構(gòu)造函數(shù)。

2)、我們再看下面的代碼

 cat.prototype = new animal();  var cat1 = new cat("haha", 'red');  console.log(cat1.constructor == animal);  //true

由上我們看到實(shí)例cat1的構(gòu)造函數(shù)是animal,所以,顯然是不對的。。。cat1明明是new cat()才生成的,所以我們應(yīng)該手動(dòng)糾正。cat.prototype對象的constructor值改為cat。

3)、所以這也是我們應(yīng)該注意的一點(diǎn),如果我們替換了prototype對象,就應(yīng)該手動(dòng)糾正prototype對象的constructor屬性。

    o.prototype = {};
    o.prototype.constructor = o;
直接繼承prototype
由于在animal對象中,不變的屬性可以直接寫在animal.prototype中。然后直接讓cat.prototype指向animal.prototype也就實(shí)現(xiàn)了繼承。

現(xiàn)在我們先將animal對象改寫成:

  function animal() {  }  animal.prototype.type = '動(dòng)物';

然后再實(shí)現(xiàn)繼承:

 cat.prototype = animal.prototype;  cat.prototype.constructor = cat;  var cat1 = new cat("haha","red");  console.log(cat1.type); // 動(dòng)物

與上一種方法相比,這種方法顯得效率更高(沒有創(chuàng)建animal實(shí)例),節(jié)省了空間。但是這樣做正確嗎?答案是不正確,我們繼續(xù)看。

    cat.prototype = animal.prototype;
這行代碼讓cat.prototype和animal.prototype指向了同一個(gè)對象,所以如果改變了cat.prototype的某一個(gè)屬性,都會(huì)反映到animal.prototype上,這顯然不是我們想要看到的。

比如我們運(yùn)行:

    console.log(animal.prototype.constructor == animal)  //false
結(jié)果看到是false,為什么呢?cat.prototype.constructor = cat;這一行就會(huì)把a(bǔ)nimal.prototype的constructor屬性也改掉了。

利用空對象作為中介

  var F = function(){};  F.prototype = animal.prototype;  cat.prototype = new F();  cat.prototype.constructor = cat;

結(jié)合上面兩種方法,因?yàn)镕是空對象,所以幾乎不占內(nèi)存。這時(shí)修改cat的prototype對象,就不會(huì)影響到animal的prototype對象。

    console.log(animal.prototype.constructor == animal);   // true
然后我們將上面的方法封裝一下:

  function extend(Child, Parent) {    var F = function(){};    F.prototype = Parent.prototype;    Child.prototype = new F();    Child.prototype.constructor = Child;    Child.uber = Parent.prototype;  }

使用的時(shí)候,方法如下:  

 extend(cat,animal);  var cat1 = new cat("haha","red");  console.log(cat1.type); // 動(dòng)物

Child.uber = Parent.prototype; 這行代碼就是個(gè)橋梁作用,讓子對象的uber屬性直接指向父對象的prototype屬性,等于在自對象上打開一條叫uber的通道,讓子對象的實(shí)例能夠使用父對象的所有屬性和方法。

以上就是對javascript對象繼承我的理解,希望或多或少能夠幫助到大家,謝謝大家的閱讀。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 中西区| 汝南县| 汽车| 余江县| 凯里市| 彭山县| 武威市| 东乌珠穆沁旗| 姜堰市| 城固县| 上栗县| 寿宁县| 英德市| 新化县| 昌都县| 新乡市| 北辰区| 文山县| 崇义县| 博罗县| 廊坊市| 肥东县| 离岛区| 台中县| 阜宁县| 密山市| 囊谦县| 中牟县| 茶陵县| 西安市| 德昌县| 无棣县| 介休市| 团风县| 岢岚县| 砀山县| 铁岭市| 南和县| 康保县| 治多县| 蓬溪县|