Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ECMAScript 6 have a convention for abstract classes? [duplicate]

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?

like image 390
obelia Avatar asked Apr 06 '15 22:04

obelia


People also ask

Can abstract class have multiple instances?

Abstract base class may be used only as framework from which other class will be derived, while the individual objects of this class will never be instantiated. Talking in terms of java, an abstract class can have any number of instances but can't have any object.

Can we extend abstract class multiple times?

In ABSTRACT class,we can't extends multiple abstract classes at a time. but In INTERFACE, we can implements multiple interfaces at time. Therefore , interfaces are used to achieve multiple inheritance in java.

Are there abstract classes in JavaScript?

Abstract Methods in JavaScriptIn an Abstract class, a type of method that is only declared and has no implementation or “function body” is known as “Abstract Method“. The abstract method must be declared in an Abstract class, whereas its definition can be added in the sub-classes.

How many times can you instantiate an abstract class?

Abstract class, we have heard that abstract class are classes which can have abstract methods and it can't be instantiated. We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.


1 Answers

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
like image 164
Domenic Avatar answered Oct 17 '22 02:10

Domenic