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

理解js對象繼承的N種模式

 更新時間:2016年01月25日 10:36:26   作者:billyangg  
這篇文章主要為大家舉例介紹了js對象繼承的幾種模式,內(nèi)容很全面,感興趣的小伙伴們可以參考一下

本文分享了js對象繼承的N種模式,供大家參考。

一、原型鏈繼承

function Person(){};

Person.prototype = {
  constructor: Person,
  name: "Oliver"
};
    
function People(){};

People.prototype = new Person();
People.prototype.constructor = People;
People.prototype.sayName = function(){
  return this.name;
};

var ins = new People();

console.log(ins.sayName());

二、借用構(gòu)造函數(shù)(偽造對象,經(jīng)典繼承)

1、無參數(shù)

function SuperType(){
  this.color = ["red","yellow","white"];
}
function SubType(){
  SuperType.call(this);
}

var instance1 = new SubType();
var instance2 = new SubType();

instance1.color.pop();
console.log(instance1.color); //["red", "yellow"]
console.log(instance2.color); //["red", "yellow", "white"]

2、有參數(shù)

function SuperType(name){
  this.name = name;
  this.number = [21,32,14,1];
}
function SubType(name,age){
  SuperType.call(this,name);
  this.age = age;
}

var instance1 = new SubType("Oliver",18);
var instance2 = new SubType("Troy",24);

instance2.number.pop();

console.log(instance1.name + instance1.age + instance1.number); //Oliver1821,32,14,1
console.log(instance2.name + instance2.age + instance2.number); //Troy2421,32,14

三、組合繼承(偽經(jīng)典繼承)

1、無參數(shù)

function SuperType(){
  this.color = ["red","yellow","white"];
}
SuperType.prototype.sayColor = function(){
  return this.color;
};

function SubType(){
  SuperType.call(this);
  this.number = 321;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayNumber = function(){
  return this.number;
};

var instance1 = new SubType();
var instance2 = new SubType();

instance2.color.pop();
console.log(instance1.color + instance1.number); //red,yellow,white321
console.log(instance2.color + instance2.number); //red,yellow321

2、有參數(shù)

function SuperType(name){
  this.name = name;
  this.number = [32,1342,11,1];
}
SuperType.prototype.sayName = function(){
  return this.name;
};

function SubType(name,age){
  SuperType.call(this,name);
  this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
  return this.age;
};

var instance1 = new SubType("Oliver",18);
var instance2 = new SubType("Troy",24);

instance2.number.pop();
console.log(instance1.sayName() + instance1.sayAge() + instance1.number); //Oliver1832,1342,11,1
console.log(instance2.sayName() + instance2.sayAge() + instance2.number); //Troy2432,1342,11

三、寄生組合式繼承(引用類型最理想的范式)

function inheritPrototype(subType,superType){
  var prototype = Object(superType.prototype);
  prototype.constructor = subType;
  subType.prototype = prototype;
}

function SuperType(name){
  this.name = name;
  this.number = [321,321,43];
}
SuperType.prototype.sayName = function(){
  return this.name;
};

function SubType(name,age){
  SuperType.call(this,name);
  this.age = age;
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge = function(){
  return this.age;
};

var instance1 = new SubType("Oliver",18);
var instance2 = new SubType("Troy",24);
instance2.number.pop();

console.log(instance1.sayName() + instance1.sayAge() + instance1.number); //Oliver18321,321,43
console.log(instance2.sayName() + instance2.sayAge() + instance2.number); //Troy24321,321

或者可以把inheritPrototype 函數(shù)寫成下面這樣:

function inheritPrototype(SubType,SuperType){
  SubType.prototype = new SuperType();
  SubType.prototype.constructor = SubType;
}

四、原型式繼承(用于共享引用類型的值,與寄生式類似)

1、傳統(tǒng)版(先定義object() 函數(shù),再繼承)

function object(o){
  function F(){};
  F.prototype = o;
  return new F();
}

var SuperType = {
  name: "Oliver",
  number: [321,321,4532,1]
};

var SubType1 = object(SuperType);
var SubType2 = object(SuperType);

SubType1.name = "Troy";
SubType1.number.pop();

SubType2.name = "Alice";
SubType2.number.pop();

console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

ECMAScript 5 版(直接用Object.create(),再繼承)

var SuperType = {
  name: "Oliver",
  number: [321,321,4532,1]
};

var SubType1 = Object.create(SuperType); //省略了定義object()函數(shù)
var SubType2 = Object.create(SuperType);

SubType1.name = "Troy";
SubType1.number.pop();

SubType2.name = "Alice";
SubType2.number.pop();

console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

ECMAScript 5 簡寫版(定義Object.create()的第二個參數(shù),再繼承)

var SuperType = {
  name: "Oliver",
  number: [321,321,4532,1]
};

var SubType1 = Object.create(SuperType,{
  name: {
    value : "Troy"
  }
});
var SubType2 = Object.create(SuperType,{
  name: {
    value : "Alice"
  }
});

SubType1.number.pop();
SubType2.number.pop();

console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

寄生式繼承(用于共享引用類型的值,與原型式類似)

function createAnother(original){
  var clone = Object(original);
  clone.sayHi = function(){
    return "Hi";
  };
  return clone;
}

var person = {
  name: "Oliver",
  number: [13,21,31,1]
};

var anotherPerson = createAnother(person);
anotherPerson.number.pop();

console.log(anotherPerson.sayHi() + anotherPerson.number); //Hi13,21,31
console.log(person.number); //13,21,31

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • 微信小程序自定義鍵盤 內(nèi)部虛擬支付

    微信小程序自定義鍵盤 內(nèi)部虛擬支付

    這篇文章主要為大家詳細介紹了微信小程序自定義鍵盤,內(nèi)部虛擬支付,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 使用JavaScript?定義自己的ajax函數(shù)

    使用JavaScript?定義自己的ajax函數(shù)

    這篇文章主要為大家介紹了JavaScript定義ajax函數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助<BR>
    2021-11-11
  • PHPExcel中的一些常用方法匯總

    PHPExcel中的一些常用方法匯總

    這篇文章主要介紹了PHPExcel中的一些常用方法匯總,本文直接給出操作代碼實例,代碼中對常用方法給出了注釋,需要的朋友可以參考下
    2015-01-01
  • 有關(guān)Promises異步問題詳解

    有關(guān)Promises異步問題詳解

    這篇文章主要介紹了有關(guān)JavaScript Promises異步問題詳解的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • 深入理解JavaScript作用域和作用域鏈

    深入理解JavaScript作用域和作用域鏈

    今天這篇文章對JavaScript作用域和作用域鏈作簡單的介紹,希望能幫助大家更好的學(xué)習(xí)JavaScript
    2011-10-10
  • JavaScript實現(xiàn)字符串與HTML格式相互轉(zhuǎn)換

    JavaScript實現(xiàn)字符串與HTML格式相互轉(zhuǎn)換

    這篇文章主要介紹了JavaScript實現(xiàn)字符串與HTML格式相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • JS閉包原理及其使用場景解析

    JS閉包原理及其使用場景解析

    這篇文章主要介紹了JS閉包原理及其使用場景解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • JavaScript數(shù)組合并案例講解

    JavaScript數(shù)組合并案例講解

    這篇文章主要介紹了JavaScript數(shù)組合并案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • p5.js臨摹旋轉(zhuǎn)愛心

    p5.js臨摹旋轉(zhuǎn)愛心

    這篇文章主要為大家詳細介紹了p5.js臨摹旋轉(zhuǎn)愛心,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 定時器(setTimeout/setInterval)調(diào)用帶參函數(shù)失效解決方法

    定時器(setTimeout/setInterval)調(diào)用帶參函數(shù)失效解決方法

    setInterval()方法可按照指定的周期(以毫秒計)來調(diào)用函數(shù)或計算表達式,setTimeout()方法用于在指定的毫秒數(shù)后調(diào)用函數(shù)或計算表達式,詳細使用方法可以參考下本文
    2013-03-03

最新評論