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 類

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,923評論 6 535
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,740評論 3 420
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,856評論 0 380
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,175評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,931評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,321評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,383評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,533評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,082評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,891評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,067評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,618評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,319評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,732評論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,987評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,794評論 3 394
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,076評論 2 375

推薦閱讀更多精彩內容