您好,登錄后才能下訂單哦!
這篇文章主要介紹“ES6怎么定義類”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“ES6怎么定義類”文章能幫助大家解決問題。
在ES6中,class(類)作為對(duì)象的模板被引入,可以通過“class”關(guān)鍵字來定義類。class的本質(zhì)是function,它可以看作一個(gè)語法糖,讓對(duì)象原型的寫法更加清晰、更像面向?qū)ο缶幊痰恼Z法。
本教程操作環(huán)境:windows7系統(tǒng)、ECMAScript 6版、Dell G3電腦。
ES6 Class
在ES6中,class(類)作為對(duì)象的模板被引入,可以通過“class”關(guān)鍵字來定義類。
class 的本質(zhì)是 function。
基本上,ES6的class可以看作只是一個(gè)語法糖,它的絕大部分功能,ES5都可以做到,新的class寫法只是讓對(duì)象原型的寫法更加清晰、更像面向?qū)ο缶幊痰恼Z法而已。
基礎(chǔ)用法
類定義
類表達(dá)式可以為匿名或命名。
// 匿名類 let Example = class { constructor(a) { this.a = a; } } // 命名類 let Example = class Example { constructor(a) { this.a = a; } }
類聲明
class Example { constructor(a) { this.a = a; } }
注意要點(diǎn):不可重復(fù)聲明。
class Example{} class Example{} // Uncaught SyntaxError: Identifier 'Example' has already been // declared let Example1 = class{} class Example{} // Uncaught SyntaxError: Identifier 'Example' has already been // declared
注意要點(diǎn):
類定義不會(huì)被提升,這意味著,必須在訪問前對(duì)類進(jìn)行定義,否則就會(huì)報(bào)錯(cuò)。
類中方法不需要 function 關(guān)鍵字。
方法間不能加分號(hào)。
new Example(); class Example {}
類的主體
屬性
prototype
ES6 中,prototype 仍舊存在,雖然可以直接自類中定義方法,但是其實(shí)方法還是定義在 prototype 上的。 覆蓋方法 / 初始化時(shí)添加方法
Example.prototype={ //methods }
添加方法
Object.assign(Example.prototype,{ //methods })
靜態(tài)屬性
靜態(tài)屬性:class 本身的屬性,即直接定義在類內(nèi)部的屬性( Class.propname ),不需要實(shí)例化。 ES6 中規(guī)定,Class 內(nèi)部只有靜態(tài)方法,沒有靜態(tài)屬性。
class Example { // 新提案 static a = 2; } // 目前可行寫法 Example.b = 2;
公共屬性
class Example{} Example.prototype.a = 2;
實(shí)例屬性
實(shí)例屬性:定義在實(shí)例對(duì)象( this )上的屬性。
class Example { a = 2; constructor () { console.log(this.a); } }
name 屬性
返回跟在 class 后的類名(存在時(shí))。
let Example=class Exam { constructor(a) { this.a = a; } } console.log(Example.name); // Exam let Example=class { constructor(a) { this.a = a; } } console.log(Example.name); // Example
方法
constructor 方法
constructor 方法是類的默認(rèn)方法,創(chuàng)建類的實(shí)例化對(duì)象時(shí)被調(diào)用。
class Example{ constructor(){ console.log('我是constructor'); } } new Example(); // 我是constructor
返回對(duì)象
class Test { constructor(){ // 默認(rèn)返回實(shí)例對(duì)象 this } } console.log(new Test() instanceof Test); // true class Example { constructor(){ // 指定返回對(duì)象 return new Test(); } } console.log(new Example() instanceof Example); // false
靜態(tài)方法
class Example{ static sum(a, b) { console.log(a+b); } } Example.sum(1, 2); // 3
原型方法
class Example { sum(a, b) { console.log(a + b); } } let exam = new Example(); exam.sum(1, 2); // 3
實(shí)例方法
class Example { constructor() { this.sum = (a, b) => { console.log(a + b); } } }
類的實(shí)例化
new
class 的實(shí)例化必須通過 new 關(guān)鍵字。
class Example {} let exam1 = Example(); // Class constructor Example cannot be invoked without 'new'
實(shí)例化對(duì)象
共享原型對(duì)象
class Example { constructor(a, b) { this.a = a; this.b = b; console.log('Example'); } sum() { return this.a + this.b; } } let exam1 = new Example(2, 1); let exam2 = new Example(3, 1); // __proto__ 已廢棄,不建議使用 // console.log(exam1.__proto__ == exam2.__proto__); console.log(Object.getPrototypeOf(exam1) === Object.getPrototypeOf(exam2));// true Object.getPrototypeOf(exam1).sub = function() { return this.a - this.b; } console.log(exam1.sub()); // 1 console.log(exam2.sub()); // 2
關(guān)于“ES6怎么定義類”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。