欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用es6 new.target來對模擬抽象類的方法

 更新時間:2019年05月10日 10:24:24   作者:jump__jump  
這篇文章主要介紹了利用es6 new.target來對模擬抽象類的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

起源

最近在使用 Symbol 來做為唯一值,發(fā)現(xiàn) Symbol 無法進行 new 操作,只能當作函數(shù)使用,只要進行了new 就會發(fā)生類型錯誤

new Symbol()

// error
Uncaught TypeError: Symbol is not a constructor
  at new Symbol (<anonymous>)
  at <anonymous>:1:1

在不考慮底層實現(xiàn)的情況下,在代碼層面是否能夠?qū)崿F(xiàn)一個函數(shù)只可以進行調(diào)用而不可以進行 new 操作呢?思考之后如下寫出:

function disConstructor() {
 if (this !== window) {
  throw new TypeError(' disConstructor is not a constructor')
 }
 console.log('gogo go')
}

// 測試結(jié)果如下
disConstructor() // gogo go

new disConstructor()

// error
Uncaught TypeError: disConstructor is not a constructor
  at new disConstructor (<anonymous>:3:15)
  at <anonymous>:1:1

如果使用 nodejs,window 可以切換為 global, 代碼運行結(jié)果不變,因為對于個人而言沒有適用場景。于是就沒有繼續(xù)研究下去,可是最近在從新翻閱 es6 時候發(fā)現(xiàn) new.target這個屬性。

new.target 屬性

介紹(引用 mdn 文檔)

new.target屬性允許你檢測函數(shù)或構(gòu)造方法是否是通過new運算符被調(diào)用的。

在通過new運算符被初始化的函數(shù)或構(gòu)造方法中,new.target返回一個指向構(gòu)造方法或函數(shù)的引用。在普通的函數(shù)調(diào)用中,new.target 的值是undefined。

這樣的話 我們的代碼就可以這樣改為

function disConstructor() {
 // 普通的函數(shù)調(diào)用中,new.target 的值是undefined。
 if (new.target) {
  throw new TypeError(' disConstructor is not a constructor')
 }
 console.log('gogo go')
}

得到與上述代碼一樣的答案。

深入

難道 es6 特地添加的功能僅僅只能用于檢查一下我們的函數(shù)調(diào)用方式嗎?

在查閱的過程各種發(fā)現(xiàn)了大多數(shù)都方案都是用 new.target 寫出只能被繼承的類。類似于實現(xiàn)java的抽象類。

class Animal {
 constructor(name, age) {
  if (new.target === Animal) {
   throw new Error('Animal class can`t instantiate');
  }
  this.name = name
  this.age = age
 }
 // 其他代碼
 ...
}

class Dog extends Animal{
 constructor(name, age, sex) {
  super(name, age)
  this.sex = sex
 }
}

new Animal()
// error
Uncaught Error: Animal class can`t instantiate
  at new Animal (<anonymous>:4:13)
  at <anonymous>:1:1

new Dog('mimi', 12, '公')
// Dog {name: "mimi", age: 12, sex: "公"}

但是 java抽象類抽象方法需要重寫,這個是沒有方案的。于是在測試與使用的過程中,卻意外發(fā)現(xiàn)了超類可以在構(gòu)造期間訪問派生類的原型,利用起來。

class Animal {
 constructor(name, age) {
  console.log(new.target.prototype)
 }
 // 其他代碼
 ...
}

之前運行時調(diào)用需要重寫的方法報錯是這樣寫的。

class Animal {
 constructor(name, age) {
  this.name = name
  this.age = age
 }

 getName () {
  throw new Error('please overwrite getName method')
 }
}

class Dog extends Animal{
 constructor(name, age, sex) {
  super(name, age)
  this.sex = sex
 }
}

const pite = new Dog('pite', 2, '公')
a.getName()
// error
Uncaught Error: please overwrite getName method
  at Dog.getName (<anonymous>:8:11)
  at <anonymous>:1:3

然而此時利用 new.target ,我就可以利用 構(gòu)造期間 對子類進行操作報錯。

class Animal {
 constructor(name, age) {
  // 如果 target 不是 基類 且 沒有 getName 報錯
  if (new.target !== Animal && !new.target.prototype.hasOwnProperty('getName')) {
   throw new Error('please overwrite getName method')
  }
  this.name = name
  this.age = age
 }
}

class Dog extends Animal{
 constructor(name, age, sex) {
  super(name, age)
  this.sex = sex
 }
}

const pite = new Dog('pite', 2, '公')
// error
Uncaught Error: please overwrite getName method
  at new Animal (<anonymous>:5:13)
  at new Dog (<anonymous>:14:5)
  at <anonymous>:1:1

此時可以把運行方法時候發(fā)生的錯誤提前到構(gòu)造時期,雖然都是在運行期,但是該錯誤觸發(fā)機制要早危害要大。反而對代碼是一種保護。

當然了,利用超類可以在構(gòu)造期間訪問派生類的原型作用遠遠不是那么簡單,必然是很強大的,可以結(jié)合業(yè)務場景談一談理解和作用。

其他方案

  • 增加 編輯器插件
  • proxy
  • 修飾器

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論