JavaScript?ES6中class定義類實例方法
前言
我們會發(fā)現(xiàn),按照之前學(xué)習(xí)過的構(gòu)造函數(shù)形式創(chuàng)建 類 ,不僅僅和編寫普通的函數(shù)過于相似,而且代碼并不容易理解。
在 ES6(ECMAScript2015)新的標(biāo)準(zhǔn)中使用了class關(guān)鍵字來直接定義類;
但是類本質(zhì)上依然是之前所學(xué)習(xí)過的構(gòu)造函數(shù)、原型鏈的語法糖而已。所以學(xué)好之前的構(gòu)造函數(shù)、原型鏈更有利于我們理解類的概念和繼承關(guān)系。
正文
1.聲明類的兩種方式:
class 關(guān)鍵字 類的聲明
class Person{}
類表達(dá)式(不常用)
var People = class { }
那么 Person類的原型是什么?如下展示了原型和typeof中Person的類型
console.log(Person.prototype) // Person {} console.log(Person.prototype.__proto__) // {} console.log(Person.constructor) // [Function: Function] console.log(typeof Person) // function
2.class 類的構(gòu)造函數(shù)
如果我們希望在創(chuàng)建對象的時候給類傳遞一些參數(shù),這個時候應(yīng)該怎么做呢?
- 每個類都可以有一個自己的構(gòu)造函數(shù)(方法),這個方法的名稱是固定的
constructor
。 - 當(dāng)我們通過new操作符,操作一個類的時候會調(diào)用這個類的構(gòu)造函數(shù)
constructor
。 - 每個類只能有一個構(gòu)造函數(shù),如果包含多個構(gòu)造函數(shù),那么會拋出異常。
示例代碼如下:
// 類的聲明 class Person { // 類的構(gòu)造方法 constructor(name, age) { this.name = name this.age = age } foo () { console.log(this.name) } } var p1 = new Person('h', 19) console.log(p1)// Person { name: 'h', age: 19 } p1.foo() // h
當(dāng)我們通過new關(guān)鍵字操作類的時候,會調(diào)用這個
constructor
函數(shù),并執(zhí)行如下操作(假設(shè)new關(guān)鍵字新創(chuàng)建的對象為p1):
- 在內(nèi)存中創(chuàng)建一個對象
- 將類的原型prototype賦值給創(chuàng)建出來的對象
p1.__proto__ = Person.prototype
- 將對象賦值給函數(shù)的this:new綁定
this = p1
- 執(zhí)行函數(shù)體中的代碼
- 自動返回創(chuàng)建出來的對象
**
3.class中方法定義
3.1 class 中定義普通的實例方法
class Person { // 類的構(gòu)造方法 constructor(name, age) { this.name = name this.age = age this._address = '北京市' } eating () { console.log(this.name + ' 正在eating~') } running () { console.log(this.name + ' 正在running~') } } var p1 = new Person('jam', 19) console.log(p1) p1.eating() p1.running()
3.2 class 類中定義訪問器方法
class Person { // 類的構(gòu)造方法 constructor(name, age) { this.name = name this.age = age this._address = '北京市' } // 類的訪問器方法 get address () { // 在這里可以設(shè)置攔截訪問操作 console.log('獲取呢') return this._address } set address (newValue) { // 在這里可以設(shè)置攔截設(shè)置操作 console.log('修改呢') return this._address = newValue } } var p1 = new Person('jam', 19) console.log(p1.address) p1.address = '天津市' console.log(p1.address)
3.3 類的靜態(tài)方法(類方法)
類的靜態(tài)方法就是在方法前加一個
static
關(guān)鍵字,該方法就成為了類的靜態(tài)方法。
類的靜態(tài)方法,不會被類的實例繼承,而是直接通過類來調(diào)用。
小案例:使用類的靜態(tài)方法完成隨機(jī)生成Person實例
class Person { // 類的構(gòu)造方法 constructor(name, age) { this.name = name this.age = age this._address = '北京市' } // 類的靜態(tài)方法(也稱為類方法) 創(chuàng)建對象隨機(jī)生成一個名字小案例 static randomPerson () { // 創(chuàng)建一個存儲名字的數(shù)組 let names = ['jam', 'jak', 'jag', 'jao', 'jno'] // Math.random()生成一個0-1之間的數(shù)字,小數(shù)肯定是不對的 let nameIndex = Math.floor(Math.random() * names.length) let name = names[nameIndex] // 生成隨機(jī)年齡 let age = Math.floor(Math.random() * 100) // return隨機(jī)生成的人物 姓名+ 年齡 return new Person(name, age) } }
這里直接調(diào)用類的靜態(tài)方法就可以 不需要使用new操作符創(chuàng)建創(chuàng)建實例對象
隨機(jī)生成一個Person實例(附效果圖)
// 隨機(jī)生成一個 var p2 = Person.randomPerson() console.log(p2)
隨機(jī)生成多個Person實例(附帶效果圖)
// 隨機(jī)生成多個 for (let index = 0; index < 20; index++) { console.log(Person.randomPerson()) }
文末
到此這篇關(guān)于JavaScript ES6中class定義類的文章就介紹到這了,更多相關(guān)ES6中class定義類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用javascript實現(xiàn)分割提取頁面所需內(nèi)容
用javascript實現(xiàn)分割提取頁面所需內(nèi)容...2007-05-05e.target與e.currentTarget對象的使用區(qū)別詳解
這篇文章主要為大家介紹了e.target與e.currentTarget的使用區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07使用dynatrace-ajax跟蹤JavaScript的性能
DynaTrace 致力于分析后臺應(yīng)用性能的表現(xiàn)已經(jīng)好幾年了,最近,他們通過發(fā)布dynaTrace Ajax Edition進(jìn)入了前端性能分析領(lǐng)域. 它是一個運(yùn)行在IE下的BHO免費(fèi)工具. 雖然我喜歡Firefox和它下面的所有插件,但我知道基于IE的測試和調(diào)試也是很重要的。2010-04-04JavaScript實現(xiàn)ASC轉(zhuǎn)漢字及漢字轉(zhuǎn)ASC的方法
這篇文章主要介紹了JavaScript實現(xiàn)ASC轉(zhuǎn)漢字及漢字轉(zhuǎn)ASC的方法,涉及JavaScript編碼轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2016-01-01