javascript中使用class和prototype的區(qū)別小結(jié)
本文將介紹在 JavaScript
何時(shí)使用class
以及何時(shí)使用prototype
。
prototype
首先先介紹一下prototype
的概念,在Javascript
中,所有的對(duì)象都從原型中繼承屬性和方法。
function Car(brand, vinNumber) { this.brand = brand; this.vinNumber = vinNumber; } Car.prototype.carInfo = function() { return { brand: this.brand, vinNumber: this.vinNumber } }
在創(chuàng)建了上面的Car
對(duì)象后,就可以使用以下的方式創(chuàng)建一個(gè)實(shí)例:
const byd = new Car('byd', 'qwuiqbasunqwidn123123');
創(chuàng)建了一個(gè)byd
實(shí)例后,就可以調(diào)用carInfo
這個(gè)方法:
console.log(byd.carInfo());
在這種情況下我們還可以通過(guò)call
或者apaply
方法來(lái)擴(kuò)展Car
對(duì)象來(lái)實(shí)現(xiàn)不同類(lèi)型的車(chē)輛。
function SportUtilityCar(brand, vinNumber, drivetrain) { Car.call(this, brand, vinNumber); this.drivetrain = drivetrain; }
現(xiàn)在,我們就可以創(chuàng)建一個(gè)SportUtilityCar
的實(shí)例而非Car
。
const byd = new SportUtilityCar('byd', 'qwuiqbasunqwidn123123', 'AWD');
而且我們還可以通過(guò)原型為SportUtilityCar
定義一個(gè)新版本的carInfo
方法。
SportUtilityCar.prototype.carInfo = function() { return { brand: this.brand, vinNumber: this.vinNumber, drivetrain: this.drivetrain } }
調(diào)用新版本的carInfo
方法:
console.log(byd.carInfo());
class
在ECMAScript 2015
中js
推出了class
這個(gè)概念。引入class
的目標(biāo)是允許使用更簡(jiǎn)單、更干凈的語(yǔ)法創(chuàng)建類(lèi)。事實(shí)上,class
只是一個(gè)“語(yǔ)法糖”,以使開(kāi)發(fā)人員能更容易編寫(xiě)代碼。
將前面用prototype
實(shí)現(xiàn)的代碼轉(zhuǎn)換成class
。
class Car { constructor(brand, vinNumber) { this.brand = brand; this.vinNumber = vinNumber; } carInfo() { return { brand: this.brand, vinNumber: this.vinNumber } } } class SportUtilityCar extends Car { constructor(brand, vinNumber, drivetrain) { super(brand, vinNumber); this.drivetrain = drivetrain; } carInfo() { return { brand: this.brand, vinNumber: this.vinNumber, drivetrain: this.drivetrain } } }
如果我們需要在類(lèi)中添加 getter
和 setter
,可以通過(guò)以下寫(xiě)法。
class SportUtilityCar extends Car { constructor(brand, vinNumber, drivetrain) { super(brand, vinNumber); this.drivetrain = drivetrain; } carInfo() { return { brand: this.brand, vinNumber: this.vinNumber, drivetrain: this.drivetrain } } get drivetrain() { return this._drivetrain; } set drivetrain(newDrivetrain) { this._drivetrain = newDrivetrain; } }
class
的語(yǔ)法類(lèi)似于 Java
或 C#
等語(yǔ)言。class
方法允許不通過(guò) Object.prototype
語(yǔ)法而為原型鏈添加屬性和方法。
class 與 prototype 對(duì)比
如上所述,JavaScript
中的類(lèi)只是語(yǔ)法糖,雖然這種方法允許那些來(lái)自Java
,C#
或C++
等語(yǔ)言的人也可以更加快速的上手javascript
,但許多Javascript
純粹主義者建議不要使用類(lèi)。
例如,Michael Krasnov
在Please stop using classes in JavaScript文章中提到一個(gè)問(wèn)題:
Binding issues. As class constructor functions deal closely with this keyword, it can introduce potential binding issues, especially if you try to pass your class method as a callback to an external routine.
翻譯: 綁定問(wèn)題。由于類(lèi)構(gòu)造函數(shù)密切處理此關(guān)鍵字,因此可能會(huì)引入潛在的綁定問(wèn)題,尤其是在嘗試將類(lèi)方法作為回調(diào)傳遞給外部程序時(shí)。
當(dāng)談到在 JavaScript
中使用類(lèi)或原型時(shí),我覺(jué)得這是一個(gè)應(yīng)該由支持和維護(hù)代碼庫(kù)的團(tuán)隊(duì)做出的決定。如果他們習(xí)慣于原型寫(xiě)法,那么就應(yīng)該合理設(shè)計(jì)他們的組件。但是,如果偏好是利用class
概念,則該團(tuán)隊(duì)的開(kāi)發(fā)人員應(yīng)該了解上述綁定挑戰(zhàn),但應(yīng)該繼續(xù)保持在他們的編程習(xí)慣。
到此這篇關(guān)于javascript中使用class和prototype的區(qū)別小結(jié)的文章就介紹到這了,更多相關(guān)javascript class和prototype內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Javascript取整函數(shù)及向零取整幾種常用的方法
這篇文章主要介紹了Javascript取整函數(shù)及向零取整幾種常用的方法,每種方法都有其特點(diǎn)和適用場(chǎng)景,推薦使用Math.trunc(),因?yàn)樗Z(yǔ)義明確、代碼易讀且性能較好,需要的朋友可以參考下2025-01-01JS實(shí)現(xiàn)的顏色實(shí)時(shí)漸變效果完整實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)的顏色實(shí)時(shí)漸變效果,結(jié)合實(shí)例形式分析了JavaScript結(jié)合時(shí)間函數(shù)定時(shí)觸發(fā)動(dòng)態(tài)改變頁(yè)面元素屬性的相關(guān)技巧,需要的朋友可以參考下2016-03-03微信小程序+mqtt,esp8266溫濕度讀取的實(shí)現(xiàn)方法
這篇文章主要介紹了微信小程序+mqtt,esp8266溫濕度讀取的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04文本框(input)獲取焦點(diǎn)(onfocus)時(shí)樣式改變的示例代碼
本篇文章主要是對(duì)文本框(input)獲取焦點(diǎn)(onfocus)時(shí)樣式改變的示例代碼進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01詳解微信圖片防盜鏈“此圖片來(lái)自微信公眾平臺(tái) 未經(jīng)允許不得引用”的解決方案
這篇文章主要介紹了詳解微信圖片防盜鏈“此圖片來(lái)自微信公眾平臺(tái) 未經(jīng)允許不得引用”的解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04