For the later workbooks, you’ll need to understand inheritance and polymorphism. The code Framework uses these features a lot. The other parts of the workbook (static and private members) are useful, and can help you understand how THREE.js works.
Recall our first
Rectangle class from last time:
When we wanted to add a
draw method to it, we had to modify the original class. But what if we want two types of
Rectangles: one which fills itself, and one which only draws the border? The simplest way we can do this is with inheritance, which we can use with the
extends keyword as follows:
super(x, y, height, width) calls the parent class (
Rectangle) constructor from inside the child class (
In a child class constructor,
super must always be called before
this is used. Basically,
this is not defined until the call to super. Suppose that we will exteded
SolidRectangle to allow it to store the color of the rectangle as well. You can use
super in methods other than the constructor, without a restriction on having it before you use
this. For example:
Because the parent constructor was called after
this was used above, we get a reference error when we try to create the class! This is easy to fix by changing the order of the lines in the constructor.
draw method works fine (we can use
super because it is not a constructor).
forEach loop (see the documentation); each object draws itself as specified by its class’s
static creationCount = 0; it is equivalent to
StaticTest.creationCount = 0, and thus
let is not necessary. This notation can be used to define class properties (and, by omitting
static, instance properties as well) outside the constructor.
The last two lines don’t work properly because they try to access a static (class) variable and method as properties of an instance of the class. Thus; as they are properties of the class and not the instance, the
creationCount property of an instance of the class does not exist; similarly, the
saySomething() method of the instance does not exist, explaining the
Private Members, Getters, and Setters
set keywords allow us to track the history of previous directories in a way which is transparent to the user. When we access
directory property of
tracker, and, similarly, when we assign
tracker.directory above, a call is made to the setter.
See the Mozilla reference page for more information.