溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

分析HTML5開發(fā)中js的創(chuàng)建和繼承

發(fā)布時(shí)間:2021-11-06 15:51:20 來源:億速云 閱讀:120 作者:iii 欄目:web開發(fā)

這篇文章主要講解了“分析HTML5開發(fā)中js的創(chuàng)建和繼承”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“分析HTML5開發(fā)中js的創(chuàng)建和繼承”吧!

  當(dāng)我們用obj.xxx訪問一個(gè)對(duì)象的屬性時(shí),JavaScript引擎先在當(dāng)前對(duì)象上查找該屬性,如果沒有找到,就到其原型對(duì)象上找,如果還沒有找到,就一直上溯到Object.prototype對(duì)象,最后,如果還沒有找到,就只能返回undefined。

  例如,創(chuàng)建一個(gè)Array對(duì)象:

  var arr = [1, 2, 3];

  其原型鏈?zhǔn)牵?/p>

  arr ----> Array.prototype ----> Object.prototype ----> null

  Array.prototype定義了indexOf()、shift()等方法,因此你可以在所有的Array對(duì)象上直接調(diào)用這些方法。

  當(dāng)我們創(chuàng)建一個(gè)函數(shù)時(shí):

  function foo() {

  return 0;

  }

  函數(shù)也是一個(gè)對(duì)象,它的原型鏈?zhǔn)牵?/p>

  foo ----> Function.prototype ----> Object.prototype ----> null

  由于Function.prototype定義了apply()等方法,因此,所有函數(shù)都可以調(diào)用apply()方法。

  很容易想到,如果原型鏈很長,那么訪問一個(gè)對(duì)象的屬性就會(huì)因?yàn)榛ǜ嗟臅r(shí)間查找而變得更慢,因此要注意不要把原型鏈搞得太長。

  構(gòu)造函數(shù)

  除了直接用{ ... }創(chuàng)建一個(gè)對(duì)象外,JavaScript還可以用一種構(gòu)造函數(shù)的方法來創(chuàng)建對(duì)象。它的用法是,先定義一個(gè)構(gòu)造函數(shù):

  function Student(name) {

  this.name = name;

  this.hello = function () {

  alert('Hello, ' + this.name + '!');

  }

  }

  你會(huì)問,咦,這不是一個(gè)普通函數(shù)嗎?

  這確實(shí)是一個(gè)普通函數(shù),但是在JavaScript中,可以用關(guān)鍵字new來調(diào)用這個(gè)函數(shù),并返回一個(gè)對(duì)象:

  var xiaoming = new Student('小明');

  xiaoming.name; // '小明'

  xiaoming.hello(); // Hello, 小明!

  注意,如果不寫new,這就是一個(gè)普通函數(shù),它返回undefined。但是,如果寫了new,它就變成了一個(gè)構(gòu)造函數(shù),它綁定的this指向新創(chuàng)建的對(duì)象,并默認(rèn)返回this,也就是說,不需要在最后寫return this;

  新創(chuàng)建的xiaoming的原型鏈?zhǔn)牵?/p>

  xiaoming ----> Student.prototype ----> Object.prototype ----> null

  也就是說,xiaoming的原型指向函數(shù)Student的原型。如果你又創(chuàng)建了xiaohong、xiaojun,那么這些對(duì)象的原型與xiaoming是一樣的:

  xiaoming ↘

  xiaohong -→Student.prototype ----> Object.prototype ----> null

  xiaojun ↗

  用new Student()創(chuàng)建的對(duì)象還從原型上獲得了一個(gè)constructor屬性,它指向函數(shù)Student本身:

  xiaoming.constructor === Student.prototype.constructor; // true

  Student.prototype.constructor === Student; // true

  Object.getPrototypeOf(xiaoming) === Student.prototype; // true

  xiaoming instanceof Student; // true

分析HTML5開發(fā)中js的創(chuàng)建和繼承

  看暈了吧?用一張圖來表示這些亂七八糟的關(guān)系就是:

  紅色箭頭是原型鏈。注意,Student.prototype指向的對(duì)象就是xiaoming、xiaohong的原型對(duì)象,這個(gè)原型對(duì)象自己還有個(gè)屬性constructor,指向Student函數(shù)本身。

  另外,函數(shù)Student恰好有個(gè)屬性prototype指向xiaoming、xiaohong的原型對(duì)象,但是xiaoming、xiaohong這些對(duì)象可沒有prototype這個(gè)屬性,不過可以用__proto__這個(gè)非標(biāo)準(zhǔn)用法來查看。

  現(xiàn)在我們就認(rèn)為xiaoming、xiaohong這些對(duì)象“繼承”自Student。

  不過還有一個(gè)小問題,注意觀察:

  xiaoming.name; // '小明'

  xiaohong.name; // '小紅'

  xiaoming.hello; // function: Student.hello()

  xiaohong.hello; // function: Student.hello()

  xiaoming.hello === xiaohong.hello; // false

  xiaoming和xiaohong各自的name不同,這是對(duì)的,否則我們無法區(qū)分誰是誰了。

  xiaoming和xiaohong各自的hello是一個(gè)函數(shù),但它們是兩個(gè)不同的函數(shù),雖然函數(shù)名稱和代碼都是相同的!

  如果我們通過new Student()創(chuàng)建了很多對(duì)象,這些對(duì)象的hello函數(shù)實(shí)際上只需要共享同一個(gè)函數(shù)就可以了,這樣可以節(jié)省很多內(nèi)存。

分析HTML5開發(fā)中js的創(chuàng)建和繼承

  要讓創(chuàng)建的對(duì)象共享一個(gè)hello函數(shù),根據(jù)對(duì)象的屬性查找原則,我們只要把hello函數(shù)移動(dòng)到xiaoming、xiaohong這些對(duì)象共同的原型上就可以了,也就是Student.prototype:

  修改代碼如下:

  function Student(name) {

  this.name = name;

  }

  Student.prototype.hello = function () {

  alert('Hello, ' + this.name + '!');

  };

  用new創(chuàng)建基于原型的JavaScript的對(duì)象就是這么簡單!

  忘記寫new怎么辦

  如果一個(gè)函數(shù)被定義為用于創(chuàng)建對(duì)象的構(gòu)造函數(shù),但是調(diào)用時(shí)忘記了寫new怎么辦?

  在strict模式下,this.name = name將報(bào)錯(cuò),因?yàn)閠his綁定為undefined,在非strict模式下,this.name = name不報(bào)錯(cuò),因?yàn)閠his綁定為window,于是無意間創(chuàng)建了全局變量name,并且返回undefined,這個(gè)結(jié)果更糟糕。

  所以,調(diào)用構(gòu)造函數(shù)千萬不要忘記寫new。為了區(qū)分普通函數(shù)和構(gòu)造函數(shù),按照約定,構(gòu)造函數(shù)首字母應(yīng)當(dāng)大寫,而普通函數(shù)首字母應(yīng)當(dāng)小寫,這樣,一些語法檢查工具如jslint將可以幫你檢測到漏寫的new。

  最后,我們還可以編寫一個(gè)createStudent()函數(shù),在內(nèi)部封裝所有的new操作。一個(gè)常用的編程模式像這樣:

  function Student(props) {

  this.name = props.name || '匿名'; // 默認(rèn)值為'匿名'

  this.grade = props.grade || 1; // 默認(rèn)值為1

  }

  Student.prototype.hello = function () {

  alert('Hello, ' + this.name + '!');

  };

  function createStudent(props) {

  return new Student(props || {})

  }

  這個(gè)createStudent()函數(shù)有幾個(gè)巨大的優(yōu)點(diǎn):一是不需要new來調(diào)用,二是參數(shù)非常靈活,可以不傳,也可以這么傳:

  var xiaoming = createStudent({

  name: '小明'

  });

  xiaoming.grade; // 1

  如果創(chuàng)建的對(duì)象有很多屬性,我們只需要傳遞需要的某些屬性,剩下的屬性可以用默認(rèn)值。由于參數(shù)是一個(gè)Object,我們無需記憶參數(shù)的順序。如果恰好從JSON拿到了一個(gè)對(duì)象,就可以直接創(chuàng)建出xiaoming。

  繼承

  在傳統(tǒng)的基于Class的語言如Java、C++中,繼承的本質(zhì)是擴(kuò)展一個(gè)已有的Class,并生成新的Subclass。

  由于這類語言嚴(yán)格區(qū)分類和實(shí)例,繼承實(shí)際上是類型的擴(kuò)展。但是,JavaScript由于采用原型繼承,我們無法直接擴(kuò)展一個(gè)Class,因?yàn)楦静淮嬖贑lass這種類型。

  但是辦法還是有的。我們先回顧Student構(gòu)造函數(shù):

  function Student(props) {

  this.name = props.name || 'Unnamed';

  }

  Student.prototype.hello = function () {

  alert('Hello, ' + this.name + '!');

分析HTML5開發(fā)中js的創(chuàng)建和繼承

  }

  以及Student的原型鏈:

  現(xiàn)在,我們要基于Student擴(kuò)展出PrimaryStudent,可以先定義出PrimaryStudent:

  function PrimaryStudent(props) {

  // 調(diào)用Student構(gòu)造函數(shù),綁定this變量:

  Student.call(this, props);

  this.grade = props.grade || 1;

  }

  但是,調(diào)用了Student構(gòu)造函數(shù)不等于繼承了Student,PrimaryStudent創(chuàng)建的對(duì)象的原型是:

  new PrimaryStudent() ----> PrimaryStudent.prototype ----> Object.prototype ----> null

  必須想辦法把原型鏈修改為:

  new PrimaryStudent() ----> PrimaryStudent.prototype ----> Student.prototype ----> Object.prototype ----> null

  這樣,原型鏈對(duì)了,繼承關(guān)系就對(duì)了。新的基于PrimaryStudent創(chuàng)建的對(duì)象不但能調(diào)用PrimaryStudent.prototype定義的方法,也可以調(diào)用Student.prototype定義的方法。

  如果你想用最簡單粗暴的方法這么干:

  PrimaryStudent.prototype = Student.prototype;

  是不行的!如果這樣的話,PrimaryStudent和Student共享一個(gè)原型對(duì)象,那還要定義PrimaryStudent干啥?

  我們必須借助一個(gè)中間對(duì)象來實(shí)現(xiàn)正確的原型鏈,這個(gè)中間對(duì)象的原型要指向Student.prototype。為了實(shí)現(xiàn)這一點(diǎn),參考道爺(就是發(fā)明JSON的那個(gè)道格拉斯)的代碼,中間對(duì)象可以用一個(gè)空函數(shù)F來實(shí)現(xiàn):

  // PrimaryStudent構(gòu)造函數(shù):

  function PrimaryStudent(props) {

  Student.call(this, props);

  this.grade = props.grade || 1;

  }

  // 空函數(shù)F:

  function F() {

  }

  // 把F的原型指向Student.prototype:

  F.prototype = Student.prototype;

  // 把PrimaryStudent的原型指向一個(gè)新的F對(duì)象,F(xiàn)對(duì)象的原型正好指向Student.prototype:

  PrimaryStudent.prototype = new F();

  // 把PrimaryStudent原型的構(gòu)造函數(shù)修復(fù)為PrimaryStudent:

  PrimaryStudent.prototype.constructor = PrimaryStudent;

  // 繼續(xù)在PrimaryStudent原型(就是new F()對(duì)象)上定義方法:

  PrimaryStudent.prototype.getGrade = function () {

  return this.grade;

  };

  // 創(chuàng)建xiaoming:

  var xiaoming = new PrimaryStudent({

  name: '小明',

  grade: 2

  });

  xiaoming.name; // '小明'

  xiaoming.grade; // 2

  // 驗(yàn)證原型:

  xiaoming.__proto__ === PrimaryStudent.prototype; // true

  xiaoming.__proto__.__proto__ === Student.prototype; // true

  // 驗(yàn)證繼承關(guān)系:

  xiaoming instanceof PrimaryStudent; // true

  xiaoming instanceof Student; // true

分析HTML5開發(fā)中js的創(chuàng)建和繼承

  用一張圖來表示新的原型鏈:

  注意,函數(shù)F僅用于橋接,我們僅創(chuàng)建了一個(gè)new F()實(shí)例,而且,沒有改變?cè)械腟tudent定義的原型鏈。

  如果把繼承這個(gè)動(dòng)作用一個(gè)inherits()函數(shù)封裝起來,還可以隱藏F的定義,并簡化代碼:

  function inherits(Child, Parent) {

  var F = function () {};

  F.prototype = Parent.prototype;

  Child.prototype = new F();

  Child.prototype.constructor = Child;

  }

  這個(gè)inherits()函數(shù)可以復(fù)用:

  function Student(props) {

  this.name = props.name || 'Unnamed';

  }

  Student.prototype.hello = function () {

  alert('Hello, ' + this.name + '!');

  }

  function PrimaryStudent(props) {

  Student.call(this, props);

  this.grade = props.grade || 1;

  }

  // 實(shí)現(xiàn)原型繼承鏈:

  inherits(PrimaryStudent, Student);

  // 綁定其他方法到PrimaryStudent原型:

  PrimaryStudent.prototype.getGrade = function () {

  return this.grade;

  };

  小結(jié)

  JavaScript的原型繼承實(shí)現(xiàn)方式就是:

  定義新的構(gòu)造函數(shù),并在內(nèi)部用call()調(diào)用希望“繼承”的構(gòu)造函數(shù),并綁定this;

  借助中間函數(shù)F實(shí)現(xiàn)原型鏈繼承,最好通過封裝的inherits函數(shù)完成;

  繼續(xù)在新的構(gòu)造函數(shù)的原型上定義新方法。

  ES6的class繼承

  新的關(guān)鍵字class從ES6開始正式被引入到JavaScript中。class的目的就是讓定義類更簡單。

  我們先回顧用函數(shù)實(shí)現(xiàn)Student的方法:

  function Student(name) {

  this.name = name;

  }

  Student.prototype.hello = function () {

  alert('Hello, ' + this.name + '!');

  }

  如果用新的class關(guān)鍵字來編寫Student,可以這樣寫:

  class Student {

  constructor(name) {

  this.name = name;

  }

  hello() {

  alert('Hello, ' + this.name + '!');

  }

  }

  比較一下就可以發(fā)現(xiàn),class的定義包含了構(gòu)造函數(shù)constructor和定義在原型對(duì)象上的函數(shù)hello()(注意沒有function關(guān)鍵字),這樣就避免了Student.prototype.hello = function () {...}這樣分散的代碼。

  最后,創(chuàng)建一個(gè)Student對(duì)象代碼和前面章節(jié)完全一樣:

  var xiaoming = new Student('小明');

  xiaoming.hello();

  class繼承

  用class定義對(duì)象的另一個(gè)巨大的好處是繼承更方便了。想一想我們從Student派生一個(gè)PrimaryStudent需要編寫的代碼量?,F(xiàn)在,原型繼承的中間對(duì)象,原型對(duì)象的構(gòu)造函數(shù)等等都不需要考慮了,直接通過extends來實(shí)現(xiàn):

  class PrimaryStudent extends Student {

  constructor(name, grade) {

  super(name); // 記得用super調(diào)用父類的構(gòu)造方法!

  this.grade = grade;

  }

  myGrade() {

  alert('I am at grade ' + this.grade);

  }

  }

  注意PrimaryStudent的定義也是class關(guān)鍵字實(shí)現(xiàn)的,而extends則表示原型鏈對(duì)象來自Student。子類的構(gòu)造函數(shù)可能會(huì)與父類不太相同,例如,PrimaryStudent需要name和grade兩個(gè)參數(shù),并且需要通過super(name)來調(diào)用父類的構(gòu)造函數(shù),否則父類的name屬性無法正常初始化。

  PrimaryStudent已經(jīng)自動(dòng)獲得了父類Student的hello方法,我們又在子類中定義了新的myGrade方法。

  ES6引入的class和原有的JavaScript原型繼承有什么區(qū)別呢?實(shí)際上它們沒有任何區(qū)別,class的作用就是讓JavaScript引擎去實(shí)現(xiàn)原來需要我們自己編寫的原型鏈代碼。簡而言之,用class的好處就是極大地簡化了原型鏈代碼。

感謝各位的閱讀,以上就是“分析HTML5開發(fā)中js的創(chuàng)建和繼承”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)分析HTML5開發(fā)中js的創(chuàng)建和繼承這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI