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

首頁 > 語言 > JavaScript > 正文

基于構造函數的五種繼承方法小結

2024-05-06 15:13:47
字體:
來源:轉載
供稿:網友

1.使用call或apply綁定構造函數

  animal.apply(this.arguments)

2.使用prototype屬性  

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

3.直接集成prototype屬性

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

4.利用空對象作為中介

  var F = function(){};  F.prototype = Animal.prototype;  Cat.prototype = new F();   Cat.prototype.constructor = Cat;  將上面的方法封裝成一個函數,便于使用:    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;  }

這個函數的作用,就是將父對象的prototype對象中的屬性,一一拷貝給Child對象的prototype對象。

以上這篇基于構造函數的五種繼承方法小結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 临沭县| 太和县| 贺兰县| 祁门县| 武平县| 织金县| 平顶山市| 乌拉特前旗| 江津市| 郯城县| 沙坪坝区| 门头沟区| 吴川市| 富阳市| 库伦旗| 榆社县| 定安县| 聂荣县| 高安市| 静乐县| 雷山县| 江都市| 淮北市| 扶沟县| 札达县| 固原市| 井研县| 沂源县| 富锦市| 伊春市| 宝山区| 东乡县| 滦平县| 西畴县| 馆陶县| 舟山市| 临桂县| 饶平县| 富平县| 庆安县| 浦县|