ES6 構(gòu)造函數(shù)語法糖:class 類

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

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

一、class

class,顧名思義,就是“類”。在ES6 之前并沒有像java、C#等語言有具體的“類”的概念,這對于面向?qū)ο箝_發(fā)是一件很難受的體驗。于是乎,ES6 為了減少 JavaScript 開發(fā)的痛苦,就提供了這么一個讓對象原型寫法更加清晰的語法糖:Class。
讓我們對比一下傳統(tǒng)構(gòu)造函數(shù)寫法,來看 class 關(guān)鍵字寫法的優(yōu)勢:

// 傳統(tǒng)寫法
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 關(guān)鍵字可以定義類,提供了更接近傳統(tǒng)語言的寫法,引入了 Class (類)這個概念作為對象的模板。
類的所有方法都是定義在類的 prototype 屬性上。

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

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

2、在類的實例上調(diào)用方法,其實就是調(diào)用原型上的方法。

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、類的內(nèi)部定義的所有方法都是不可枚舉。
5、類的調(diào)用必須要使用 new 命令,否則會報錯。
6、Class 表達(dá)式
與函數(shù)一樣,Class 也可以使用表達(dá)式的形式定義。

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

7、采用 Class 表達(dá)式,可以寫出立即執(zhí)行的 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 的指向
類的方法內(nèi)部如果含有 this,它將默認(rèn)指向類的實例,如果將該方法提出出來單獨(dú)使用,this 指向的是該方法運(yùn)行時所在的環(huán)境,找不到該方法,會報錯:

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

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

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

方法二:使用箭頭函數(shù):

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 關(guān)鍵字

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

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 關(guān)鍵字

如果在一個方法錢加上 static 關(guān)鍵字,就表示該方法不會被實例繼承,而是通過類調(diào)用,成為靜態(tài)方法。

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

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

父類的靜態(tài)方法可以被子類繼承:

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

Bar.classMethod(); // hello

靜態(tài)方法也可以從 super 對象上調(diào)用:

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

Bar.classMethod(); // hello, too

四、extends 關(guān)鍵字

在之前的 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 關(guān)鍵字實現(xiàn)原型繼承:

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

五、super 關(guān)鍵字

當(dāng)你想在子類中調(diào)用父類 的函數(shù)時,super 關(guān)鍵字就很有作用了,使用這個關(guān)鍵字時應(yīng)該注意:
使用 super 關(guān)鍵字的時候,必須顯示指定是作為函數(shù)還是作為對象使用,否則會報錯。
1、super 作為函數(shù)調(diào)用時,代表父類的構(gòu)造函數(shù)。ES6 中要求,子類的構(gòu)造函數(shù)必須執(zhí)行一次 super 函數(shù)。

class A { }

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

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

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 調(diào)用父類的方法時,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();

六、其它補(bǔ)充

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

七、令人遐想的 Mixin 模式的實現(xiàn)

所謂 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 函數(shù)可以將多個對象合成一個類。使用方法如下:

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

結(jié)語

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

戳我博客

章節(jié)目錄

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • class的基本用法 概述 JavaScript語言的傳統(tǒng)方法是通過構(gòu)造函數(shù),定義并生成新對象。下面是一個例子: ...
    呼呼哥閱讀 4,194評論 3 11
  • Class 的基本語法 簡介 JavaScript 語言中,生成實例對象的傳統(tǒng)方法是通過構(gòu)造函數(shù)。下面是一個例子。...
    huilegezai閱讀 601評論 0 0
  • 官方中文版原文鏈接 感謝社區(qū)中各位的大力支持,譯者再次奉上一點點福利:阿里云產(chǎn)品券,享受所有官網(wǎng)優(yōu)惠,并抽取幸運(yùn)大...
    HetfieldJoe閱讀 3,720評論 2 27
  • 繼承6種套餐 參照紅皮書,JS繼承一共6種 1.原型鏈繼承 核心思想:子類的原型指向父類的一個實例 Son.pro...
    燈不梨喵閱讀 3,251評論 1 2
  • 開學(xué)已是第二天,在這兩天里娘兩為作業(yè)尤其是寫字拌了多次嘴,氣的我肚子咕咕的。學(xué)習(xí)態(tài)度很不端正,尤其是寫字...
    解浩宇媽媽閱讀 272評論 2 0

友情鏈接更多精彩內(nèi)容