June 2018
Beginner
288 pages
6h 31m
English
When creating abstractions, we often arrive at methods that may not be specific to any particular instance. Sometimes they are general and sometimes they may deal with multiple instances. A class method, instead of an instance method, is used to design these.
JavaScript makes it easy to define static methods and static properties. It does not, however, provide an elegant way to define static fields. Let’s continue with our Car class to add a few static members.
First, let’s add a static field to the class. For this, we have to step out of the class definition, like so:
| | Car.distanceFactor = 0.01; //This goes outside class Car {...} |
We may also define property getters (and setters if we like), and those go within the ...
Read now
Unlock full access