-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_obj_constr_new.js
39 lines (27 loc) · 1.21 KB
/
js_obj_constr_new.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
MAKE INSTANCES OF OJBECTS WITH A CONSTRUCTOR FUNCTION
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:
{
wheels: 4,
engines: 1,
seats: 1
}
Note that it is important to use the new keyword when calling a constructor. This is how Javascript knows to create a new object and that all the references to this inside the constructor should be referring to this new object.
Now, once the myCar 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";
Our myCar variable now has a property turboType with a value of "twin".
In the editor, use the Car constructor to create a new instance and assign it to myCar.
Then give myCar a nickname property with a string value.
*/
var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 1;
};
// Only change code below this line.
var myCar = new Car();
myCar.nickname = "Pet Dragon";
console.log("My Car: ", myCar);