javascript創(chuàng)建對象、對象繼承的實(shí)用方式詳解
本文約定:不特殊聲明的情況下,屬性代指屬性或方法。
創(chuàng)建對象、對象繼承實(shí)際上是一回事:我們所需要的實(shí)例對象通過構(gòu)造函數(shù)獲得私有屬性、通過原型鏈獲得共享的屬性。什么是好的方式?私有屬性通過構(gòu)造函數(shù)的方式獲得(不考慮實(shí)例中自定義私有屬性)且不需要重寫,共享屬性通過原型鏈找到且不需要重復(fù)創(chuàng)建。
普適的方式
組合使用構(gòu)造函數(shù)模式和原型模式創(chuàng)建對象
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');
通過字面量的方式會重寫prototype,且原型的constructor指向了Object,必要的情況下需要重新定義constructor。
寄生組合式繼承
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ù)傳入的對象變成實(shí)例的原型,該對象的屬性被所有實(shí)例共享。
共享屬性:inheritPrototype(Student_2011, HNU_student);,子構(gòu)造函數(shù)原型成為超構(gòu)造函數(shù)原型的一個實(shí)例,超構(gòu)造函數(shù)原型中的屬性共享給子構(gòu)造函數(shù)。
私有屬性:HNU_student.call(this, name);,通過子構(gòu)造函數(shù)創(chuàng)建實(shí)例時調(diào)用超構(gòu)造函數(shù)創(chuàng)建私有屬性。
創(chuàng)建對象的其他方式
動態(tài)原型模式
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ù)中,使用判斷語句,在第一次調(diào)用構(gòu)造函數(shù)創(chuàng)建實(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ù)添加特殊的屬性。
對象繼承的其他方式
組合繼承
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(); 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í)例通過原型鏈找到所有共享的屬性。
私有屬性:HNU_student.call(this, name);,通過子構(gòu)造函數(shù)創(chuàng)建實(shí)例時調(diào)用超構(gòu)造函數(shù)創(chuàng)建私有屬性。
缺陷:超構(gòu)造函數(shù)被調(diào)用了兩遍。Student_2011.prototype = new HNU_student();的同時,在子構(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()是ECMAScript5新增的方法,接受兩個參數(shù):一是作為原型的原對象,二是重寫或新增屬性的對象,作用與自定義的object()相同。
var student1 = { name: 'xujian', school: 'HNU' }; var student2 = Object.create(student1, { name: { value: 'huangjing' } });
寄生式繼承在原型式繼承的基礎(chǔ)上添加了額外的屬性用來增強(qiáng)對象。
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)建與已有對象類似的實(shí)例對象。
相關(guān)文章
javascript獲取函數(shù)名稱、函數(shù)參數(shù)、對象屬性名稱的代碼實(shí)例
這篇文章主要介紹了javascript獲取函數(shù)名稱、函數(shù)參數(shù)、對象屬性的代碼實(shí)例,需要的朋友可以參考下2014-04-04javascript中的3種繼承實(shí)現(xiàn)方法
這篇文章主要介紹了javascript中的3種繼承實(shí)現(xiàn)方法,包括使用Object.create實(shí)現(xiàn)類式繼承、使用utilities工具包自帶的util.inherites、使用extends關(guān)鍵字,非常的實(shí)用,希望對大家了解javascript繼承能夠有所幫助2016-01-01JavaScript起點(diǎn)(嚴(yán)格模式深度了解)
嚴(yán)格模式(Strict Mode)是ECMAScript5新增的功能,目前所有的主流瀏覽器的最新版本——包括IE10與Opera12——都支持嚴(yán)格模式,感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01JavaScript 學(xué)習(xí)筆記(十二) dom
新的一章的學(xué)習(xí)----Dom,重點(diǎn)啊2010-01-01