最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間: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()的第二個(gè)參數(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)文章

  • javascript代碼簡寫的幾種常用方式匯總

    javascript代碼簡寫的幾種常用方式匯總

    任何一種編程語言的簡寫小技巧都是為了幫助你寫出更簡潔、更完善的代碼,讓你用更少的編碼實(shí)現(xiàn)你的需求,這篇文章主要給大家介紹了關(guān)于javascript代碼簡寫的幾種常用方式,需要的朋友可以參考下
    2021-08-08
  • JS實(shí)現(xiàn)網(wǎng)頁右側(cè)帶動(dòng)畫效果的伸縮窗口代碼

    JS實(shí)現(xiàn)網(wǎng)頁右側(cè)帶動(dòng)畫效果的伸縮窗口代碼

    這篇文章主要介紹了JS實(shí)現(xiàn)網(wǎng)頁右側(cè)帶動(dòng)畫效果的伸縮窗口代碼,通過JavaScript基于時(shí)間函數(shù)實(shí)現(xiàn)頁面元素樣式漸變效果,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 微信小程序如何監(jiān)聽全局變量

    微信小程序如何監(jiān)聽全局變量

    這篇文章主要給大家介紹了關(guān)于微信小程序如何監(jiān)聽全局變量的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • js刪除所有的cookie的代碼

    js刪除所有的cookie的代碼

    有時(shí)候需要?jiǎng)h除網(wǎng)站的cookies,一個(gè)一個(gè)太麻煩,這個(gè)可以批量的刪除所有的cookies,需要的朋友可以參考下。
    2010-11-11
  • 簡單易用的倒計(jì)時(shí)js代碼

    簡單易用的倒計(jì)時(shí)js代碼

    倒計(jì)時(shí)js代碼想必大家都有用過,大同小異,本例為大家介紹的是簡單易用的,需要的朋友可以參考下
    2014-08-08
  • JavaScript在Android的WebView中parseInt函數(shù)轉(zhuǎn)換不正確問題解決方法

    JavaScript在Android的WebView中parseInt函數(shù)轉(zhuǎn)換不正確問題解決方法

    這篇文章主要介紹了JavaScript在Android的WebView中parseInt函數(shù)轉(zhuǎn)換不正確問題解決方法,因轉(zhuǎn)換的字符串?dāng)?shù)字都以0開頭,導(dǎo)致parseInt函數(shù)在瀏覽器和Android WebView中轉(zhuǎn)換結(jié)果不一樣,本文給出了解決方法,需要的朋友可以參考下
    2015-04-04
  • underscore之Chaining_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    underscore之Chaining_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    本文通過文字說明與代碼的形式給大家介紹了underscore之Chaining的相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧
    2017-07-07
  • javascript 控制彈出窗口

    javascript 控制彈出窗口

    javascript 控制彈出窗口...
    2007-04-04
  • 基于js實(shí)現(xiàn)微信發(fā)送好友如何分享到朋友圈、微博

    基于js實(shí)現(xiàn)微信發(fā)送好友如何分享到朋友圈、微博

    微信瀏覽器內(nèi)置了javascript私有對象WeixinJSBridge,可以實(shí)現(xiàn)發(fā)送給朋友、分享到朋友圈、分享到微博等功能,本篇文章給大家介紹基于js實(shí)現(xiàn)微信發(fā)送給朋友如何分享到朋友圈、微博,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • 完美解決JS文件頁面加載時(shí)的阻塞問題

    完美解決JS文件頁面加載時(shí)的阻塞問題

    下面小編就為大家?guī)硪黄昝澜鉀QJS文件頁面加載時(shí)的阻塞問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12

最新評(píng)論

桂林市| 师宗县| 紫阳县| 黔东| 湘潭市| 二连浩特市| 潞西市| 都江堰市| 肇东市| 农安县| 霍州市| 贵定县| 濉溪县| 鄂托克前旗| 东源县| 南康市| 夏津县| 罗田县| 定州市| 永清县| 新巴尔虎右旗| 温州市| 金塔县| 三河市| 巨鹿县| 淳化县| 三门峡市| 临邑县| 阜阳市| 六枝特区| 五大连池市| 嘉义市| 伊金霍洛旗| 辉县市| 英山县| 龙岩市| 芮城县| 通河县| 台南县| 新平| 永靖县|