摘要:創(chuàng)建對(duì)象對(duì)象繼承實(shí)際上是一回事我們所需要的實(shí)例對(duì)象通過(guò)構(gòu)造函數(shù)獲得私有屬性通過(guò)原型鏈獲得共享的屬性。原型式繼承和寄生式繼承用于創(chuàng)建與已有對(duì)象類似的實(shí)例對(duì)象。
本文約定:不特殊聲明的情況下,屬性代指屬性或方法。
創(chuàng)建對(duì)象、對(duì)象繼承實(shí)際上是一回事:我們所需要的實(shí)例對(duì)象通過(guò)構(gòu)造函數(shù)獲得私有屬性、通過(guò)原型鏈獲得共享的屬性。什么是好的方式?私有屬性通過(guò)構(gòu)造函數(shù)的方式獲得(不考慮實(shí)例中自定義私有屬性)且不需要重寫(xiě),共享屬性通過(guò)原型鏈找到且不需要重復(fù)創(chuàng)建。
普適的方式 組合使用構(gòu)造函數(shù)模式和原型模式創(chuàng)建對(duì)象function HNU_student(name) { this.name = name; this.sayName = function() { return this.name; }; } HNU_student.prototype = { school: "HNU", saySchool: function() { return this.school; } }; Object.defineProperty(HNU_student, "constructor", {value: HNU_student}); var hiyohoo = new HNU_student("xujian");
通過(guò)字面量的方式會(huì)重寫(xiě)prototype,且原型的constructor指向了Object,必要的情況下需要重新定義constructor,使用Object.defineProperty()方法可以將constructor的enumerable等特性變?yōu)槌跏寄J(rèn)的false。
寄生組合式繼承function object(o) { function F() {}; F.prototype = o; return new F(); } function inheritPrototype(child, parent) { var prototype = object(parent.prototype); prototype.constructor = child; child.prototype = prototype; } function HNU_student(name) { this.name = name; this.sayName = function() { return this.name; }; } HNU_student.prototype.school = "HNU"; HNU_student.prototype.saySchool = function() { return this.school; }; function Student_2011(name, number) { HNU_student.call(this, name); this.number = number; this.sayNumber = function() { return this.number; } } inheritPrototype(Student_2011, HNU_student); Student_2011.prototype.graduationTime = 2015; Student_2011.prototype.sayGraduationTime = function() { return this.graduationTime; }; var hiyohoo = new Student_2011("xujian", 20110803203);
object()的作用:將作為參數(shù)傳入的對(duì)象變成實(shí)例的原型,該對(duì)象的屬性被所有實(shí)例共享。
共享屬性:inheritPrototype(Student_2011, HNU_student);,子構(gòu)造函數(shù)原型成為超構(gòu)造函數(shù)原型的一個(gè)實(shí)例,超構(gòu)造函數(shù)原型中的屬性共享給子構(gòu)造函數(shù)。
私有屬性:HNU_student.call(this, name);,通過(guò)子構(gòu)造函數(shù)創(chuàng)建實(shí)例時(shí)調(diào)用超構(gòu)造函數(shù)創(chuàng)建私有屬性。
function HNU_student(name) { this.name = name; this.sayName = function() { return this.name; }; if (!HNU_student.prototype.school) { HNU_student.prototype.school = "HNU"; HNU_student.prototype.saySchool = function() { return this.school; }; } } var hiyohoo = new HNU_student("xujian");
將定義在原型中的共享屬性放入構(gòu)造函數(shù)中,使用判斷語(yǔ)句,在第一次調(diào)用構(gòu)造函數(shù)創(chuàng)建實(shí)例時(shí),初始化原型共享屬性。
寄生構(gòu)造函數(shù)模式function SpecialArray() { var values = new Array(); values.push.apply(values, arguments); values.toPipedString = function() { return this.join("|"); }; return values; } var colors = new SpecialArray("red", "black", "white");
用于為原生構(gòu)造函數(shù)添加特殊的屬性。
對(duì)象繼承的其他方式 組合繼承function HNU_student(name) { this.name = name; this.sayName = function() { return this.name; }; } HNU_student.prototype.school = "HNU"; HNU_student.prototype.saySchool = function() { return this.school; }; function Student_2011(name, number) { HNU_student.call(this, name); this.number = number; this.sayNumber = function() { return this.number; }; } Student_2011.prototype = new HNU_student(); //重寫(xiě)原型一定要放在所有原型屬性定義之前 Student_2011.prototype.constructor = Student_2011; Student_2011.prototype.graduationTime = 2015; Student_2011.prototype.sayGraduationTime = function() { return this.graduationTime; } var hiyohoo = new Student_2011("xujian", 20110803203);
共享屬性:Student_2011.prototype = new HNU_student();,子構(gòu)造函數(shù)的原型就指向了超構(gòu)造函數(shù)的原型,實(shí)例通過(guò)原型鏈找到所有共享的屬性。
私有屬性:HNU_student.call(this, name);,通過(guò)子構(gòu)造函數(shù)創(chuàng)建實(shí)例時(shí)調(diào)用超構(gòu)造函數(shù)創(chuàng)建私有屬性。
缺陷:超構(gòu)造函數(shù)被調(diào)用了兩遍。Student_2011.prototype = new HNU_student();的同時(shí),在子構(gòu)造函數(shù)原型中創(chuàng)建了超構(gòu)造函數(shù)定義的私有屬性,這些原型中的私有屬性被實(shí)例中的同名屬性覆蓋屏蔽。
原型式繼承、寄生式繼承function object(o) { function F() {} F.prototype = o; return new F(); } var student1 = { school: "HNU", saySchool: function() { return this.school; } }; var student2 = object(student1);
Object.creat()是ECMAScript 5新增的方法,接受兩個(gè)參數(shù):一是作為原型的原對(duì)象,二是重寫(xiě)或新增屬性的對(duì)象,作用與自定義的object()相同。
var student1 = { name: "xujian", school: "HNU" }; var student2 = Object.create(student1, { name: { value: "huangjing" } });
寄生式繼承在原型式繼承的基礎(chǔ)上添加了額外的屬性用來(lái)增強(qiáng)對(duì)象。
function object(o) { function F() {} F.prototype = o; return new F(); } function creatAnother(original) { var clone = object(original); clone.sayHi = function() { alert("Hi!"); }; return clone; } var student1 = { school: "HNU", saySchool: function() { return this.school; } }; var student2 = creatAnother(student1);
原型式繼承和寄生式繼承用于創(chuàng)建與已有對(duì)象類似的實(shí)例對(duì)象。
轉(zhuǎn)載請(qǐng)注明出處:https://segmentfault.com/a/1190000004559437
文章不定期更新完善,如果能對(duì)你有一點(diǎn)點(diǎn)啟發(fā),我將不勝榮幸。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/78829.html
摘要:學(xué)編程真的不是一件容易的事不管你多喜歡或是多會(huì)編程,在學(xué)習(xí)和解決問(wèn)題上總會(huì)碰到障礙。熟練掌握核心內(nèi)容,特別是和多線程初步具備面向?qū)ο笤O(shè)計(jì)和編程的能力掌握基本的優(yōu)化策略。 學(xué)Java編程真的不是一件容易的事,不管你多喜歡或是多會(huì)Java編程,在學(xué)習(xí)和解決問(wèn)題上總會(huì)碰到障礙。工作的時(shí)間越久就越能明白這個(gè)道理。不過(guò)這倒是一個(gè)讓人進(jìn)步的機(jī)會(huì),因?yàn)槟阋恢辈粩嗟膶W(xué)習(xí)才能很好的解決你面前的難題...
摘要:與方法這兩個(gè)方法的第一個(gè)參數(shù)是一個(gè)對(duì)象,是給準(zhǔn)備的,接著在調(diào)用函數(shù)時(shí)將其綁定到。作為事件處理函數(shù)當(dāng)函數(shù)被用作事件處理函數(shù)時(shí),它的會(huì)指向觸發(fā)事件的元素??偨Y(jié)在任何函數(shù)體外部,無(wú)論是否在嚴(yán)格模式,都指代全局對(duì)象。 this在JavaScript中似乎可以視而不見(jiàn),但不去正視它學(xué)到的就只是殘缺的JS。拋開(kāi)這些形而上的意義不說(shuō),從實(shí)用性及代碼簡(jiǎn)潔展示this的不可或缺的地位,舉例子: func...
摘要:構(gòu)造函數(shù)實(shí)例和原型的概念和關(guān)系每個(gè)函數(shù)都屬于對(duì)象,都會(huì)有一個(gè)屬性叫。這也是繼承的依據(jù)。這樣一來(lái),原型找不到構(gòu)造函數(shù),這是非常蛋疼的事情,違反了原型鏈的定義啊。所以現(xiàn)在子對(duì)象原型和父對(duì)象原型是就建立關(guān)系了。 構(gòu)造函數(shù)、實(shí)例和原型的概念和關(guān)系 每個(gè)函數(shù)都屬于對(duì)象,都會(huì)有一個(gè)屬性叫prototype。這個(gè)屬性指向一個(gè)對(duì)象,我們把他叫做當(dāng)前函數(shù)的原型對(duì)象。原型對(duì)象下面有個(gè)屬性叫construc...
摘要:避免脆弱的基類問(wèn)題。紅牌警告沒(méi)有提到上述任何問(wèn)題。單向數(shù)據(jù)流意味著模型是單一的事實(shí)來(lái)源。單向數(shù)據(jù)流是確定性的,而雙向綁定可能導(dǎo)致更難以遵循和理解的副作用。原文地址 1. 你能說(shuō)出兩種對(duì) JavaScript 應(yīng)用開(kāi)發(fā)者而言的編程范式嗎? 希望聽(tīng)到: 2. 什么是函數(shù)編程? 希望聽(tīng)到: 3. 類繼承和原型繼承的不同? 希望聽(tīng)到 4. 函數(shù)式編程和面向?qū)ο缶幊痰膬?yōu)缺點(diǎn)? ...
摘要:在創(chuàng)建子類實(shí)例時(shí),不能向超類型的構(gòu)造函數(shù)中傳遞參數(shù)。構(gòu)造函數(shù)繼承子類傳進(jìn)的值是基本思想是在子類構(gòu)造函數(shù)的內(nèi)部調(diào)用超類或父類型構(gòu)造函數(shù)。繼承保證構(gòu)造函數(shù)指針指向如果想同時(shí)繼承多個(gè),還可使用添加屬性的方式類繼承, OOP:Object Oriented Programming 面向?qū)ο缶幊獭?題外話:面向?qū)ο蟮姆秶鷮?shí)在太大,先把這些大的東西理解理解。 1.什么是對(duì)象? 根據(jù)高程和權(quán)威指南上...
閱讀 2750·2023-04-25 20:28
閱讀 1947·2021-11-22 09:34
閱讀 3774·2021-09-26 10:20
閱讀 1945·2021-09-22 16:05
閱讀 3150·2021-09-09 09:32
閱讀 2599·2021-08-31 09:40
閱讀 2177·2019-08-30 13:56
閱讀 3378·2019-08-29 17:01