Now let's put that great
constructor function we made in the last lesson to use!
To use a
constructor function we call it with the
new keyword in front of it like:
var myCar = new Car();
myCar is now an
instance of the
Car constructor that looks like the object it described:
Note that it is important to use the
this inside the constructor should be referring to this new object.
Now, once the
instance is created it can be used like any other object and can have its properties accessed and modified the same way you would usually. For example:
myCar.turboType = "twin";
myCar variable now has a property
turboType with a value of
In the editor, use the
constructor to create a new
instance and assign it to
nickname property with a string value.