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

首頁 > 開發(fā) > JS > 正文

基于構(gòu)造函數(shù)的五種繼承方法小結(jié)

2024-05-06 16:38:41
字體:
供稿:網(wǎng)友

1.使用call或apply綁定構(gòu)造函數(shù)

  animal.apply(this.arguments)

2.使用prototype屬性  

  Cat.prototype = new Animal();  Cat.prototype.constructor = Cat;  var cat1 = new Cat("大毛","黃色");  alert(cat1.species); // 動(dòng)物

3.直接集成prototype屬性

  function Animal(){ }  Animal.prototype.species = "動(dòng)物";   Cat.prototype = Animal.prototype;  Cat.prototype.constructor = Cat;  var cat1 = new Cat("大毛","黃色");  alert(cat1.species); // 動(dòng)物

4.利用空對(duì)象作為中介

  var F = function(){};  F.prototype = Animal.prototype;  Cat.prototype = new F();   Cat.prototype.constructor = Cat;  將上面的方法封裝成一個(gè)函數(shù),便于使用:    function extend(Child, Parent) {      var F = function(){};      F.prototype = Parent.prototype;      Child.prototype = new F();      Child.prototype.constructor = Child;      Child.uber = Parent.prototype;    }

5.拷貝繼承

function extend2(Child, Parent) {    var p = Parent.prototype;    var c = Child.prototype;    for (var i in p) {      c[i] = p[i];      }    c.uber = p;  }

這個(gè)函數(shù)的作用,就是將父對(duì)象的prototype對(duì)象中的屬性,一一拷貝給Child對(duì)象的prototype對(duì)象。

以上這篇基于構(gòu)造函數(shù)的五種繼承方法小結(jié)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 遵化市| 连山| 绿春县| 长春市| 武功县| 视频| 平乐县| 永州市| 万全县| 临潭县| 广州市| 镇安县| 大连市| 云梦县| 孝昌县| 万年县| 辽阳县| 泊头市| 山阳县| 北海市| 宜昌市| 龙岩市| 蒲江县| 连城县| 博乐市| 贡觉县| 靖江市| 襄城县| 汉沽区| 凌海市| 桓台县| 泾阳县| 隆尧县| 太白县| 巴楚县| 琼结县| 杂多县| 曲周县| 仙桃市| 手机| 东宁县|