首頁  >  文章  >  web前端  >  ES6透過什麼定義類

ES6透過什麼定義類

青灯夜游
青灯夜游原創
2022-03-09 18:52:281496瀏覽

在ES6中,class(類別)作為物件的模板被引入,可以透過「class」關鍵字來定義類別。 class的本質是function,它可以看成一個語法糖,讓物件原型的寫法更加清晰、更像物件導向程式設計的語法。

ES6透過什麼定義類

本教學操作環境:windows7系統、ECMAScript 6版、Dell G3電腦。

ES6 Class

在ES6中,class(類別)作為物件的模板被引入,可以透過「class」關鍵字來定義類。

class 的本質是 function。

基本上,ES6的class可以看成只是一個語法糖,它的絕大部分功能,ES5都可以做到,新的class寫法只是讓物件原型的寫法更加清晰、更像物件導向程式設計的語法而已。

基礎用法

類別定義

##類別表達式可以匿名或命名。

// 匿名类
let Example = class {
    constructor(a) {
        this.a = a;
    }
}
// 命名类
let Example = class Example {
    constructor(a) {
        this.a = a;
    }
}

類別宣告

class Example {
    constructor(a) {
        this.a = a;
    }
}

注意要點:不可重複宣告。

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

注意要點:

類別定義不會被提升,這意味著,必須在存取前對類別進行定義,否則就會報錯。

類別中方法不需要 function 關鍵字。

方法間不能加分號。

new Example(); 
class Example {}

類別的主體

#屬性

prototype

ES6 中,prototype 仍舊存在,雖然可以直接自類別中定義方法,但其實方法還是定義在prototype 上的。覆寫方法/ 初始化時新增方法

Example.prototype={
    //methods
}

新增方法

Object.assign(Example.prototype,{
    //methods
})

靜態屬性

靜態屬性:class 本身的屬性,即直接定義在類別內部的屬性( Class. propname ),不需要實例化。 ES6 中規定,Class 內部只有靜態方法,沒有靜態性質。

class Example {
// 新提案
    static a = 2;
}
// 目前可行写法
Example.b = 2;

公有屬性

class Example{}
Example.prototype.a = 2;

實例屬性

實例屬性:定義在實例物件( this )上的屬性。

class Example {
    a = 2;
    constructor () {
        console.log(this.a);
    }
}

name 屬性

傳回跟在 class 後的類別名稱(存在時)。

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 方法是類別的預設方法,在建立類別的實例化物件時被呼叫。

class Example{
    constructor(){
      console.log('我是constructor');
    }
}
new Example(); // 我是constructor

回傳物件

class Test {
    constructor(){
        // 默认返回实例对象 this
    }
}
console.log(new Test() instanceof Test); // true
 
class Example {
    constructor(){
        // 指定返回对象
        return new Test();
    }
}
console.log(new Example() instanceof Example); // false

靜態方法

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

實例方法

class Example {
    constructor() {
        this.sum = (a, b) => {
            console.log(a + b);
        }
    }
}

#類的實例化

new

class 的實例化必須透過new 關鍵字。

class Example {}
 
let exam1 = Example(); 
// Class constructor Example cannot be invoked without 'new'

實例化物件

共享原型物件

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

【相關推薦:

javascript影片教學web前端

以上是ES6透過什麼定義類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn