Javascript ECMAScript 6 是否有抽象类的约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29480569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:26:25  来源:igfitidea点击:

Does ECMAScript 6 have a convention for abstract classes?

javascriptoopabstract-classecmascript-6

提问by obelia

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).

我很惊讶在阅读 ES6 时我找不到任何关于抽象类的信息。(我所说的“抽象类”是指 Java 的含义,其中抽象类声明子类必须实现的方法签名才能实例化)。

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.

有谁知道在 ES6 中实现抽象类的任何约定?能够通过静态分析捕获抽象类违规会很好。

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

如果我在运行时引发错误以表示尝试抽象类实例化,错误会是什么?

回答by Domenic

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.

ES2015 没有带有内置可供性的 Java 风格类,用于您所需的设计模式。但是,它有一些可能有用的选项,具体取决于您要完成的任务。

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

如果您想要一个无法构造但其子类可以构造的类,那么您可以使用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

有关 的更多详细信息new.target,您可能需要阅读 ES2015 中的类如何工作的一般概述: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