forked from LaunchCodeEducation/javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassExamples02.js
More file actions
34 lines (21 loc) · 1 KB
/
Copy pathClassExamples02.js
File metadata and controls
34 lines (21 loc) · 1 KB
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
// Use terminal commands to see what happens when we call Astronaut
// but do not pass in 3 arguments.
//The code returns "undefined" for the missing argument
// Next, set default values for 1 or more of the parameters in constructor.
// The defaults we set (this.age = 90, this.mass=100, solved the "undefined"
// output for this.mass, but overrode the input of 120 for age.)
class Astronaut {
constructor(name, age, mass){
this.name = name;
this.age = age;
this.mass = mass;
}
}
let tortoise = new Astronaut('Speedy', 120);
console.log(tortoise.name, tortoise.age, tortoise.mass);
console.log(typeof tortoise.age);
// What happens if we call Astronaut and pass in MORE than 3 arguments? TRY IT!
// First I restored the defaults this.age to age and this.mass to mass.
let tortoise2 = new Astronaut('Flash', 190, 4651, "Plumber");
console.log(tortoise2.name, tortoise2.age, tortoise2.mass, tortoise2.role);
// The extra value, "Plumber," did not print. Instead the output was "undefined"