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