Quiz
- In Shape class, I tested ""this.constructor.name"" in the console, why does it show ""Window"" instead of the class name ""Shape""? Where does ""Window"" come from?
First, bravo to you for testing stuff in the JS console! That's excellent. We all need to be willing to experiment and try stuff out.
It turns out that there is global variable called
windowthat contains a JS object literal (a dictionary) containing all the global variables, including itself.Anyhow, the keyword
thisis typically bound towindow. Try:When OOP code is used in the usual way, the
thiskeyword is bound to the object at hand, but apparently that didn't happen in the way that you used it. Try:But inside a constructor,
thiswill have the correct value, namely the object being initialized. - what exactly is the purpose of "super"?
To allow a class to refer to its super-class. This is most important in the constructor. The constructor for
Rectangleneeds to call the constructor forShape, so it usessuperto do that. see Shapes Example - Can you explain more about the concept abstraction barrier?
For sure! Imagine we are using the in-memory
DataStore, which just uses the following methods:addgetgetAllremove
Now, we happen to know that internally, there's a dictionary, so instead of using
getandaddto increment a value, we decide to reach inside and do a+=on the dictionary. Our code works and saves us a little bit of time.Later, the project manager decides to swap out the in-memory
DataStorefor the cloud-basedRemoteDataStoremodule.The coffeerun code breaks horribly, and the manager eventually finds the culprit: it was us and our violation of the abstraction barrier. We didn't stick to the prescribed API (the published methods) and so the project broke.
- all clear! / There are no topics I would like to talk about.
Great!