ES6 構造函數語法糖:class 類

為了解決ES5 中原型鏈繼承實現給我們造成的麻煩,ES6 又給我們提供了一顆語法糖:Class。

本文將通過以下幾個關鍵字:class、constructor、static、extends、super 來具體了解一下 Class。

一、class

class,顧名思義,就是“類”。在ES6 之前并沒有像java、C#等語言有具體的“類”的概念,這對于面向對象開發是一件很難受的體驗。于是乎,ES6 為了減少 JavaScript 開發的痛苦,就提供了這么一個讓對象原型寫法更加清晰的語法糖:Class。
讓我們對比一下傳統構造函數寫法,來看 class 關鍵字寫法的優勢:

// 傳統寫法
function Animal(type, name) {
  this.type = type;
  this.name = name;
}

Animal.prototype.toString = function () {
  return '(' + this.type + ',' + this.name + ')';
};
var m = new Animal('monkey', 'yuan');

// class 寫法
class Animal {
  constructor (type, name) {
    this.type = type;
    this.name = name;
  }
  toString() {
    return '(' + this.type + ',' + this.name + ')';
  }
}
var m = new Animal('monkey', 'yuan');
m.toString(); // (monkey,yuan)

1、通過 class 關鍵字可以定義類,提供了更接近傳統語言的寫法,引入了 Class (類)這個概念作為對象的模板。
類的所有方法都是定義在類的 prototype 屬性上。

class Animal {
  constructor() { ... };
  toString() { ... };
  getName() { ... };
}

// 等價于
Animal.prototype = {
  constructor() {},
  toString() {},
  getName() {}
}

2、在類的實例上調用方法,其實就是調用原型上的方法。

class A {};
let a = new b();
a.constructor === a.prototype.constructor; // true

3、由于類的方法(除 constructor 之外)都定義在 prototype 對象上,所以類的新方法可以添加在 prototype 對象上。Object.assgn() 方法可以很方便的一次向類添加多個方法。

class Animal {
  constructor () { ... };
}
Object.assign(Animal.prototype, {
  toString() { ... },
  getName() { ... }
});

4、類的內部定義的所有方法都是不可枚舉。
5、類的調用必須要使用 new 命令,否則會報錯。
6、Class 表達式
與函數一樣,Class 也可以使用表達式的形式定義。

const MyClass = class Me {
  getClassName() {
    return Me.name;
  }
};

7、采用 Class 表達式,可以寫出立即執行的 class。

let animal = new class {
  constructor(name) {
    this.name = name;
  }
  
  sayName() {
    console.log(this.name);
  }
}("monkey");

animal.sayName(); // monkey

8、與 ES5 不同,類不存在變量提升

new Foo(); // ReferenceError
class Foo {};

9、this 的指向
類的方法內部如果含有 this,它將默認指向類的實例,如果將該方法提出出來單獨使用,this 指向的是該方法運行時所在的環境,找不到該方法,會報錯:

class Animal {
  printName (name = "monkey") {
    this.print(`Hello ${name}`);
  }
  print(name) {
    console.log(name);
  }
}
const animal = new Animal();
const { printName } = animal;
printName(); // 報錯

如何解決類方法中的 this 指向問題呢?
方法一:在構造方法中綁定 this:

class Animal {
  constructor () {
    this.printName = this.printName.bind(this);
  }
  // ...
}

方法二:使用箭頭函數:

class Animal {
  constructor () {
    this.printName = ( name = "monkey" ) => {
      this.print(`Hello ${ name }`);
    };
  }
// ....
}

方法三:使用 Proxy,在獲取方法的時候自動綁定 this:

function selfish (target) {
  const cache = new WeakMap();
  const handle = {
    get (target, key) {
      const value = Reflect.get(target, key) {
       if (typeof value !== 'function') return value;
       if (!cache.has(value)) cache.set(value, value.bind(target));
      retrun cache.get(value); 
      }
    };
    const proxy = new Proxy(target, handler);
    return proxy;
  }
}
const animal = selfish(new Animal());

二、constructor 關鍵字

上面第一段代碼中的 constructor 方法,是構造方法,this 關鍵字則代表實例對象。
一個類必須要有 constructor 方法,如果沒有顯示定義,一個空的 constructor 方法會被默認添加

class Animal { }
// 等同于
class Animal {
  constructor() {}
}

實例的屬性除非顯式定義在其本身(即定義在this對象上),否則都是定義在原型上(即定義在class上),并且,類的所有實例共享一個原型對象。

class Person {

  //自身屬性
  constructor( name , age ) {
    this.name = name;
    this.age = age;
  }
  
  //原型對象的屬性
  say() {
    return 'My name is ' + this.name + ', I am ' + this.age + ' years old';
  }
}

var person = new Person( 'Jack' , 23);
person.hasOwnProperty('name')                   // true
person.hasOwnProperty('age')                    // true
person.hasOwnProperty('say')                    // false
person.__proto__.hasOwnProperty('say')          // true

上述代碼中,name 和 age 實例對象person自身的屬性(因為定義在this變量上) ,所以hasOwnProperty方法返回true,而say是原型對象的屬性(因為定義在Person類上),所以hasOwnProperty方法返回false。

三、static 關鍵字

如果在一個方法錢加上 static 關鍵字,就表示該方法不會被實例繼承,而是通過類調用,成為靜態方法。

class Foo {
  static calssMethod() {
    return 'hello';
  }
}
Foo.classMethod(); // hello

var foo = new Foo();
foo.classMethod(); // TypeError: foo.calssMethod is not function

父類的靜態方法可以被子類繼承:

class Foo {
   static classMethod() {
    return 'hello';
  }
}
class Bar extends Foo { }

Bar.classMethod(); // hello

靜態方法也可以從 super 對象上調用:

class Foo {
  static classMethod() {
    return 'hello';
  }
}
class Bar extends Foo {
  static classMethod() {
   return super.classMethod() + ', too'; 
  }
}

Bar.classMethod(); // hello, too

四、extends 關鍵字

在之前的 ES5 中,處理原型的繼承非常麻煩,我們先看一下 ES5 是如何處理原型繼承的:

function Monkey(type, name) {
  Animal.apply(this, [type, name]);
}

Monkey.prototype = Object.create(Animal.prototype, {
  toSting: function() {
    return "monkey is" + tjhis.type + ",name is " + this.name;
  }
};
Monkey.prototype.constructor = Monkey;

在 ES6 中使用 extends 關鍵字實現原型繼承:

class Monkeyi extends Animal {
  constructor(type, name) {
    super(type, name);
  }
  toString() {
    return  "monkey is" `${this.type}` ",name is "`{ this.name}`;
  }
}

五、super 關鍵字

當你想在子類中調用父類 的函數時,super 關鍵字就很有作用了,使用這個關鍵字時應該注意:
使用 super 關鍵字的時候,必須顯示指定是作為函數還是作為對象使用,否則會報錯。
1、super 作為函數調用時,代表父類的構造函數。ES6 中要求,子類的構造函數必須執行一次 super 函數。

class A { }

class B extends A {
  f() {
    super(); // 報錯
  }
}

2、super 作為對象時,在普通方法中,指向父類的原型對象;在靜態方法中,指向父類。

class Parent {
  static myMethod(msg) {
    console.log('static');
  }

  myMethod(msg) {
    console.log('instance');
  }
}

class Child extends Parent {
  static myMethod() {
    super.myMethod();
  }

  myMethod(msg) {
    super.myMethod();
  }
}

Child.myMethod(); // static 

var child = new Child();
child.myMethod(); // instance

3、通過 super 調用父類的方法時,super 會綁定子類的 this:

class A {
  constructor() {
    this.x = 1;
  }
  print() {
    console.log(this.x);
  }
}

class B extends A {
  constructor() {
    super();
    this.x = 2;
  }
  m() {
    super.print();
  }
}

let b = new B();
b.m() // 2

4、由于綁定子類的 this,因此通過 super 對某個屬性賦值,這是 super 就是 this,賦值的屬性會變成子類實例的屬性:

class A {
  constructor() {
    this.x = 1;
  }
}

class B extends A {
  constructor() {
    super();
    this.x = 2;
    super.x = 3;
    console.log(super.x); // undefined
    console.log(this.x); // 3
  }
}

let b = new B();

六、其它補充

除了以上幾個關鍵字的介紹,如要了解如 new.target 屬性、私有屬性等,請參看阮一峰的《ES6 標準入門(第三版)》書籍,或參考 class 的基本語法

七、令人遐想的 Mixin 模式的實現

所謂 Mixin 模式:將多個類的接口“混入”(mix in)另一個類。如下:

funtion mix(...mixins) {
  class Mix {}

  for (let mixin of mixins) {
    copyProperties(Mix, mixin);
    copyProperties(Mix.prototype, mixin.prototype);
  }
  
  return Mix;
}

function copyProperties(target, source) {
  for (let key of Reflect.ownKeys(source)) {
    if( key !== "constructor"
      && key !== "prototype"
      && key !== "name"
    ) {
         let desc = Object.getOwnPropertyDescriptor(source, key);
         Object.defineProperty(target, key, desc): 
      }
  }
}

上述代碼中的 Mix 函數可以將多個對象合成一個類。使用方法如下:

class DistributeEdit extends mix(Loggable, Serializable) {
  ...
}

結語

本章主要是以ES6 類的五個關鍵字為主,粗略的學習了,在 ES6 中是如何定義類,以及其用法,再則就是類的繼承。對于類還需進一步的理解,本文比較粗糙,后續會進行相關專題的深入學習。如若有錯,歡迎拍磚。

戳我博客

章節目錄

1、ES6中啥是塊級作用域?運用在哪些地方?
2、ES6中使用解構賦值能帶給我們什么?
3、ES6字符串擴展增加了哪些?
4、ES6對正則做了哪些擴展?
5、ES6數值多了哪些擴展?
6、ES6函數擴展(箭頭函數)
7、ES6 數組給我們帶來哪些操作便利?
8、ES6 對象擴展
9、Symbol 數據類型在 ES6 中起什么作用?
10、Map 和 Set 兩數據結構在ES6的作用
11、ES6 中的Proxy 和 Reflect 到底是什么鬼?
12、從 Promise 開始踏入異步操作之旅
13、ES6 迭代器(Iterator)和 for...of循環使用方法
14、ES6 異步進階第二步:Generator 函數
15、JavaScript 異步操作進階第三步:async 函數
16、ES6 構造函數語法糖:class 類

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容