ECMAScript 6是否有抽象类的约定? [英] Does ECMAScript 6 have a convention for abstract classes?

查看:192
本文介绍了ECMAScript 6是否有抽象类的约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶,在ES6上阅读时,我找不到任何关于抽象类的内容。 (抽象类我正在谈论Java的含义,其中一个抽象类声明一个子类必须实现的方法签名,以便可实例化)。



<有谁知道在ES6中实现抽象类的任何约定?如果我在运行时提出一个错误来表示抽象类实例化的尝试,那么可以通过静态分析来捕获抽象类违例。



错误是什么?

解决方案

ES2015没有适合您所需设计模式的内置可用性的Java风格的类。但是,它有一些可能有帮助的选项,具体取决于您正在尝试完成的任务。



如果您想要一个无法构造的类,但其子类可以,那么你可以使用 new.target

  class Abstract { 
构造函数(){
if(new.target ===摘要){
抛出新的TypeError(不能直接构造抽象实例);
}
}
}

class Derived extends Abstract {
constructor(){
super();
//更多这里的派生特定的东西,也许
}
}

const a = new Abstract(); // new.target是Abstract,所以它抛出
const b = new Derived(); // new.target是派生的,所以没有错误

有关 new.target ,您可能需要阅读ES2015课程的总体概述: http://www.2ality.com/2015/02/es6-classes-final.html



如果你专门看要实现某些方法,您可以在超类构造函数中检查:

  class Abstract {
constructor (){
if(this.method === undefined){
//或者可能是test.of this.method ===function
throw new TypeError(必须覆盖方法 );
}
}
}

class Derived1 extends Abstract {}

class Derived2 extends Abstract {
method(){ }
}

const a = new Abstract(); // this.method是未定义的错误
const b = new Derived1(); // this.method是未定义的错误
const c = new Derived2(); // this.method是Derived2.prototype.method;没有错误


I was surprised that I couldn't find anything about abstract classes when reading up on ES6. (By "abstract class" I'm talking about the Java meaning of it, in which an abstract class declares method signatures that a subclass must implement in order to be instantiable).

Does anyone know of any conventions that have taken hold to implement abstract classes in ES6? It would be nice to be able to catch an abstract class violation with static analysis.

If I were to raise an error at runtime to signal an attempt at abstract class instantiation, what would the error be?

解决方案

ES2015 does not have Java-style classes with built-in affordances for your desired design pattern. However, it has some options which may be helpful, depending on exactly what you are trying to accomplish.

If you would like a class that cannot be constructed, but whose subclasses can, then you can use new.target:

class Abstract {
  constructor() {
    if (new.target === Abstract) {
      throw new TypeError("Cannot construct Abstract instances directly");
    }
  }
}

class Derived extends Abstract {
  constructor() {
    super();
    // more Derived-specific stuff here, maybe
  }
}

const a = new Abstract(); // new.target is Abstract, so it throws
const b = new Derived(); // new.target is Derived, so no error

For more details on new.target, you may want to read this general overview of how classes in ES2015 work: http://www.2ality.com/2015/02/es6-classes-final.html

If you're specifically looking for requiring certain methods be implemented, you can check that in the superclass constructor as well:

class Abstract {
  constructor() {
    if (this.method === undefined) {
      // or maybe test typeof this.method === "function"
      throw new TypeError("Must override method");
    }
  }
}

class Derived1 extends Abstract {}

class Derived2 extends Abstract {
  method() {}
}

const a = new Abstract(); // this.method is undefined; error
const b = new Derived1(); // this.method is undefined; error
const c = new Derived2(); // this.method is Derived2.prototype.method; no error

这篇关于ECMAScript 6是否有抽象类的约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆