MENU
Abstract Class
An abstract classabstract class abstract class , sometimes also called a templatetemplate template , is a class from which an object cannot be instantiated directly.
As of July 2017, there is no native support for abstract classes and interfaces. However, you can do the following:
RESETRUNFULL
<!DOCTYPE html><html><body><script>
class Food{
constructor(b){
if (new.target === Food) // or:
this.contructor === Food
throw new TypeError("Cannot instantiate from an Abstract Class directly! Food");
if (this.brand === undefined)
throw new TypeError("brand() must be defined -- Food");
}}class DogFood extends Food{}//var h = new Food; // TypeError: Cannot instantiate from an Abstract Class directly! -- Foodvar o = new DogFood; // TypeError: brand() must be defined – Food
</script></body><html>
Every time a 'new' keyword is used to instantiate an object (eg. new Food), the class (ie. Food)is assigned to a meta property, new.targetnew.target new.target , which can be accessed within the constructor.