-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (63 loc) · 1.45 KB
/
index.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Create a Person
function Person(first, last, age, gender, interests) {
this.name = {
first,
last,
};
this.age = age;
this.gender = gender;
this.interests = interests;
}
//Add greeting function on constructor's prototype.
Person.prototype.greeting = function () {
alert("Hi! I'm " + this.name.first + ".");
};
//Now, Define a Teacher
function Teacher(first, last, age, gender, interests, subject) {
this.name = {
first,
last,
};
this.age = age;
this.gender = gender;
this.interests = interests;
this.subject = subject;
}
//As a note, we could have done this. These are the same !
function Teacher(first, last, age, gender, interests, subject) {
Person.call(this, first, last, age, gender, interests);
this.subject = subject;
}
//Giving Teacher greeting function on constructor's prototype.
Teacher.prototype.greeting = function () {
let prefix;
if (this.gender === "male") {
prefix = "Mr.";
} else if (this.gender === "female") {
prefix = "Ms.";
} else {
prefix = "Mx.";
}
alert(
"Hello. My name is " +
prefix +
" " +
this.name.last +
", and I teach " +
this.subject +
"."
);
};
//Try this example
let teacher1 = new Teacher(
"John",
"Smith",
28,
"male",
["basketball", "music"],
"geometry"
);
teacher1.name.first; // Inherited from Person() constructor
teacher1.interests[0]; // Inherited from Person() constructor
teacher1.subject;
teacher1.greeting();