How it works...

The goal of this was to force users of the class to extend the Rocket class, rather than instantiate it directly. However, we still want to be able to define behavior in the base class. In the constructor of the Rocket class you can see how this is done. By comparing the constructor property of the instance with Rocket we can see if this has been instantiated directly. If the class is extended, then this comparison will evaluate false and the error will not be throw. Thus, we can create ActiveRocket and InactiveRocket instances.

When creating an instance of Rocket directly, the constructor comparison evaluates to true and the error is thrown. Thus, we can't create instances of the Rocket class, only it's sub-classes.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.