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

javascript創(chuàng)建對(duì)象、對(duì)象繼承的實(shí)用方式詳解

 更新時(shí)間:2016年03月08日 08:55:19   作者:hiYoHoo  
JavaScript中的對(duì)象是基于原型的。原型是對(duì)象的基礎(chǔ),它定義并實(shí)現(xiàn)了一個(gè)新對(duì)象所必須包含的成員列表,并被所有同類(lèi)對(duì)象實(shí)例所共享。與其他語(yǔ)言中類(lèi)的概念相比,原型更像是類(lèi)的靜態(tài)成員。本文著重給大家介紹javascript創(chuàng)建對(duì)象、對(duì)象繼承的實(shí)用方式

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

創(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。

寄生組合式繼承

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)建私有屬性。

創(chuàng)建對(duì)象的其他方式

動(dò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ù)中,使用判斷語(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();
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()是ECMAScript5新增的方法,接受兩個(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ì)象類(lèi)似的實(shí)例對(duì)象。

相關(guān)文章

  • JavaScript DOM 學(xué)習(xí)第五章 表單簡(jiǎn)介

    JavaScript DOM 學(xué)習(xí)第五章 表單簡(jiǎn)介

    在這一章我主要介紹一些用來(lái)檢測(cè)用戶(hù)輸入的代碼,利用這些代碼,你也可以寫(xiě)一些自己的檢測(cè)函數(shù)。
    2010-02-02
  • 常用一些Javascript判斷函數(shù)

    常用一些Javascript判斷函數(shù)

    常用一些Javascript判斷函數(shù),都是一些js基礎(chǔ)知識(shí),學(xué)習(xí)js的朋友可以看下
    2012-08-08
  • 現(xiàn)代 JavaScript 參考

    現(xiàn)代 JavaScript 參考

    一份現(xiàn)代 JavaScript 參考,你在現(xiàn)代項(xiàng)目中會(huì)經(jīng)常遇到,以及最新的代碼示例,需要的朋友可以參考下
    2017-10-10
  • 了解JavaScript函數(shù)中的默認(rèn)參數(shù)

    了解JavaScript函數(shù)中的默認(rèn)參數(shù)

    JavaScript函數(shù)可以有默認(rèn)參數(shù)值。通過(guò)默認(rèn)函數(shù)參數(shù),你可以初始化帶有默認(rèn)值的正式參數(shù)。下面我們來(lái)一起學(xué)習(xí)一下吧
    2019-05-05
  • JavaScript顯式數(shù)據(jù)類(lèi)型轉(zhuǎn)換詳解

    JavaScript顯式數(shù)據(jù)類(lèi)型轉(zhuǎn)換詳解

    這篇文章主要介紹了JavaScript顯式數(shù)據(jù)類(lèi)型轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 詳解javascript函數(shù)寫(xiě)法大全

    詳解javascript函數(shù)寫(xiě)法大全

    這篇文章主要介紹了javascript函數(shù)寫(xiě)法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 詳解javascript設(shè)計(jì)模式三:代理模式

    詳解javascript設(shè)計(jì)模式三:代理模式

    這篇文章主要介紹了javascript設(shè)計(jì)模式三:代理模式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • javascript scrollLeft,scrollWidth,clientWidth,offsetWidth 完全詳解

    javascript scrollLeft,scrollWidth,clientWidth,offsetWidth 完全

    javascript scrollLeft,scrollWidth,clientWidth,offsetWidth 完全詳解,實(shí)例修正版。
    2009-07-07
  • JavaScript在XHTML中的用法詳解

    JavaScript在XHTML中的用法詳解

    下面的代碼在HTML中是有效的,但在XHTML中則是無(wú)效的接下來(lái)為大家介紹下JavaScript在XHTML中的用法,感興趣的朋友可以參考下哈
    2013-04-04
  • javascript關(guān)于繼承解析

    javascript關(guān)于繼承解析

    下面小編就為大家全面的介紹一下javascript在的繼承。希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05

最新評(píng)論

比如县| 保德县| 武城县| 平南县| 普陀区| 宜城市| 吉林省| 米易县| 特克斯县| 嘉兴市| 随州市| 泽州县| 拉萨市| 喀什市| 内江市| 波密县| 民和| 榆中县| 赤城县| 太和县| 南安市| 正安县| 厦门市| 沧源| 晋宁县| 新干县| 潼关县| 平泉县| 柘荣县| 江北区| 金秀| 老河口市| 历史| 潞城市| 三穗县| 黔西县| 葵青区| 达拉特旗| 宽甸| 福州市| 清河县|