Managing Instance Types Rules
Explore how to manage instance types in JavaScript inheritance by implementing methods that dynamically choose the constructor based on runtime types or static class hints. Understand the nuances of creating new instances in base and derived classes to maintain correct typing.
We'll cover the following...
Sticking to the base type
We’ll create a base class and a derived class and learn ways to manage the types of instances the base methods create.
Example
Let’s start with a class named Names and a derived SpecializedNames class.
The base class constructor receives a rest parameter of names and stores that into a field named names. The derived class does not have any methods, and its default constructor will faithfully pass parameters to its base class.
filter() method
Let’s implement a filter1() method, in the Names class, that will return an instance with only selected names.
filter1(selector) {
return new Names(...this.names.filter(selector));
}
The filter1() method receives a function reference, selector, as a parameter. It passes the reference to the filter() method of the names array and, finally, creates an instance of Names using the result of the call to filter(). The filter1() method hardcoded the class name in new Names(). Therefore, the instance returned by filter1() will be an instance of Names even if the method is called on the derived instance.
Sticking to the base type in action
Let’s verify this.
We created an instance specializedNames of SpecializedNames, invoked the filter1() method on that instance, and printed the response ...