Left Hand Side
In JavaScript, you may hear the expressions "left side" or "right side" when talking about declarations. These refer to evaluations or comparisons, and to which side the particular expression is on. (eg let x /*left side*/ = 12 /*right side*/
) Here we will focus on the left hand side, and specifically the keywords new
and super
.
new
new
is a keyword that lets a user create an object based off of a user-designed model. The model must be created first as a function, like this:
this.score
refers to the property of the object being created, while score
refers to the parameter being passed into the function. We can create a new Game object with the following:
super
Sometimes the information inside of an object is so large that we store it inside another object within the first. This is called "nesting". Think of it like a news program:
news
is the parent object, whereas sports
and weather
are the child objects. Unfortunately for our news program, the commercial break function lies in the parent object, meaning that the children can't go to commercial. We use the super
keyword in these situations to allow the child objects to access properties of the parent. The newscasters can just call the method themselves (this.throwToCommercial('weather', 3)
), but if the weatherman tried to use this
, he'd get an error. The weatherman has to go to break like this:
File Location
We will be working in the following file:
Practice
Create a function to be used as a model for today's date, then use that model to create an object called today.
Add a property to the today object called tomorrow (
let today.tomorrow = ...
).Using the news example from above, add the propery
localNews
to the object, then have the sportscaster go to commercial, teasing the local news coming up in 3 minutes.
Last updated