javascript建立對象、對象繼承的實用方式詳解_基礎知識

來源:互聯網
上載者:User

本文約定:不特殊聲明的情況下,屬性代指屬性或方法。

建立對象、對象繼承實際上是一回事:我們所需要的執行個體對象通過建構函式獲得私人屬性、通過原型鏈獲得共用的屬性。什麼是好的方式?私人屬性通過建構函式的方式獲得(不考慮執行個體中自訂私人屬性)且不需要重寫,共用屬性通過原型鏈找到且不需要重複建立。

普適的方式

組合使用建構函式模式和原型模式建立對象

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()的作用:將作為參數傳入的對象變成執行個體的原型,該對象的屬性被所有執行個體共用。

共用屬性:inheritPrototype(Student_2011, HNU_student);,子建構函式原型成為超建構函式原型的一個執行個體,超建構函式原型中的屬性共用給子建構函式。
私人屬性:HNU_student.call(this, name);,通過子建構函式建立執行個體時調用超建構函式建立私人屬性。

建立對象的其他方式

動態原型模式

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');

將定義在原型中的共用屬性放入建構函式中,使用判斷語句,在第一次調用建構函式建立執行個體時,初始化原型共用屬性。

寄生建構函式模式

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');

用於為原生建構函式添加特殊的屬性。

對象繼承的其他方式

組合繼承

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();,子建構函式的原型就指向了超建構函式的原型,執行個體通過原型鏈找到所有共用的屬性。
私人屬性:HNU_student.call(this, name);,通過子建構函式建立執行個體時調用超建構函式建立私人屬性。

缺陷:超建構函式被調用了兩遍。Student_2011.prototype = new HNU_student();的同時,在子建構函式原型中建立了超建構函式定義的私人屬性,這些原型中的私人屬性被執行個體中的同名屬性覆蓋屏蔽。

原型式繼承、寄生式繼承

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新增的方法,接受兩個參數:一是作為原型的原對象,二是重寫或新增屬性的對象,作用與自訂的object()相同。

var student1 = {  name: 'xujian',  school: 'HNU'};var student2 = Object.create(student1, {  name: {    value: 'huangjing'  }});

寄生式繼承在原型式繼承的基礎上添加了額外的屬性用來增強對象。

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);

原型式繼承和寄生式繼承用於建立與已有對象類似的執行個體對象。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.