利用 es6 new.target 来对模拟抽象类

起源

最近在使用 Symbol 来作为惟一值,发现 Symbol 没法进行 new 操做,只能看成函数使用,只要进行了new 就会发生类型错误java

new Symbol()

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

在不考虑底层实现的状况下,在代码层面是否可以实现一个函数只能够进行调用而不能够进行 new 操做呢?思考以后以下写出:node

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

// 测试结果以下
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, 代码运行结果不变,由于对于我的而言没有适用场景。因而就没有继续研究下去,但是最近在重新翻阅 es6 时候发现 new.target这个属性。es6

new.target 属性

介绍(引用 mdn 文档)

new.target属性容许你检测函数或构造方法是不是经过new运算符被调用的。
在经过new运算符被初始化的函数或构造方法中,new.target返回一个指向构造方法或函数的引用。在普通的函数调用中,new.target 的值是undefined。bash

这样的话 咱们的代码就能够这样改成编辑器

function disConstructor() {
  // 普通的函数调用中,new.target 的值是undefined。
  if (new.target) {
    throw new TypeError(' disConstructor is not a constructor')
  }
  console.log('gogo go')
}
复制代码

获得与上述代码同样的答案。函数

深刻

难道 es6 特意添加的功能仅仅只能用于检查一下咱们的函数调用方式吗?
在查阅的过程各类发现了大多数都方案都是用 new.target 写出只能被继承的类。相似于实现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抽象类抽象方法须要重写,这个是没有方案的。因而在测试与使用的过程当中,却意外发现了超类能够在构造期间访问派生类的原型,利用起来。this

class Animal {
  constructor(name, age) {
    console.log(new.target.prototype)
  }
  // 其余代码
  ...
}
复制代码

以前运行时调用须要重写的方法报错是这样写的。spa

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 ,我就能够利用 构造期间 对子类进行操做报错。prototype

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
复制代码

此时能够把运行方法时候发生的错误提早到构造时期,虽然都是在运行期,可是该错误触发机制要早危害要大。反而对代码是一种保护。

固然了,利用超类能够在构造期间访问派生类的原型做用远远不是那么简单,必然是很强大的,能够结合业务场景谈一谈理解和做用。

其余方案

增长 编辑器插件 proxy 修饰器

相关文章
相关标签/搜索