摘要:原型鏈繼承借用構(gòu)造函數(shù)偽造對(duì)象,經(jīng)典繼承無(wú)參數(shù)有參數(shù)組合繼承偽經(jīng)典繼承無(wú)參數(shù)有參數(shù)寄生組合式繼承引用類(lèi)型最理想的范式或者可以把函數(shù)寫(xiě)成下面這樣原型式繼承用于共享引用類(lèi)型的值,與寄生式類(lèi)似傳統(tǒng)版先定義函數(shù),再繼承版直接用,再繼承省略了定義函數(shù)
原型鏈繼承
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ù)(偽造對(duì)象,經(jīng)典繼承) 無(wú)參數(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"]有參數(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)典繼承) 無(wú)參數(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有參數(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寄生組合式繼承(引用類(lèi)型最理想的范式)
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ù)寫(xiě)成下面這樣:
function inheritPrototype(SubType,SuperType){ SubType.prototype = new SuperType(); SubType.prototype.constructor = SubType; }原型式繼承(用于共享引用類(lèi)型的值,與寄生式類(lèi)似) 傳統(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,321ECMAScript 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,321ECMAScript 5 簡(jiǎn)寫(xiě)版(定義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寄生式繼承(用于共享引用類(lèi)型的值,與原型式類(lèi)似)
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
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/86262.html
摘要:工廠模式不推薦應(yīng)該把方法放在函數(shù)的外面,避免重復(fù)創(chuàng)建該方法定義的不是構(gòu)建函數(shù),因該使用方法創(chuàng)建實(shí)例,而不是方法不要忘記在函數(shù)的最后構(gòu)造函數(shù)模式不推薦使用指代,函數(shù)無(wú)需明確應(yīng)該把方法放在函數(shù)的外面,避免重復(fù)創(chuàng)建該方法原型模式不推薦函數(shù)中不對(duì)屬 工廠模式(不推薦) var sayName = function(){ return this.name; }; function cr...
摘要:二用操作符構(gòu)造對(duì)象屬性名屬性值屬性名屬性值屬性名屬性值屬性名屬性值方法名方法名首先用創(chuàng)建一個(gè)空對(duì)象,然后用多條語(yǔ)句給對(duì)象添加屬性方法。他的寫(xiě)法與三用函數(shù)聲明的方式構(gòu)造對(duì)象比較像,但是稍有不同。 -- 新手向知識(shí),就不用ES6寫(xiě)法了。 一、字面量聲明 var obj = { 屬性名1 : 屬性值, 屬性名2 : 屬性值, 屬性名3 : 屬性...
摘要:使用最多的繼承模式是組合繼承,這種模式使用原型鏈繼承共享的屬性和方法,而借用構(gòu)造函數(shù)繼承實(shí)例屬性。原型式繼承,可以在不必預(yù)先定義構(gòu)造函數(shù)的情況下實(shí)現(xiàn)繼承,其本質(zhì)是執(zhí)行給定對(duì)象的淺復(fù)制。 1、原型鏈實(shí)現(xiàn)繼承 function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = func...
摘要:當(dāng)解釋器尋找引用值時(shí),會(huì)首先檢索其在棧中的地址,取得地址后從堆中獲得實(shí)體如何實(shí)現(xiàn)繼承構(gòu)造繼承原型繼承實(shí)例繼承拷貝繼承原型機(jī)制或和方法去實(shí)現(xiàn)較簡(jiǎn)單,建議使用構(gòu)造函數(shù)與原型混合方式。它是基于的一個(gè)子集。 JavaScript介紹js的基本數(shù)據(jù)類(lèi)型。Undefined、Null、Boolean、Number、Stri...
摘要:這里存在內(nèi)存泄露問(wèn)題,油畫(huà)后的代碼如下這里這里這里這里這里這里 原始代碼: function Cars(){ this.name = Benz; this.color = [white,black]; } Cars.prototype.sayColor = function(){ var outer = this; return function(){ ...
閱讀 1450·2021-11-25 09:43
閱讀 2591·2021-09-24 10:30
閱讀 3665·2021-09-06 15:02
閱讀 3601·2019-08-30 15:55
閱讀 3305·2019-08-30 15:53
閱讀 1700·2019-08-30 15:52
閱讀 2147·2019-08-30 14:21
閱讀 2013·2019-08-30 13:55