forked from truecodersio/JavaScript_OOP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
148 lines (103 loc) · 2.82 KB
/
app.js
File metadata and controls
148 lines (103 loc) · 2.82 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
console.log("Hello World!\n==========\n");
// Exercise 1 Section
console.log("EXERCISE 1:\n==========\n");
class Person {
//Classes are reusable templates for objects.
constructor(name, pets, residence, hobbies) {
//Constructor makes obj properties.
this.name = name;
this.pets = pets;
this.residence = residence;
this.hobbies = hobbies;
}
addHobby(hobby) {
this.hobbies.push(hobby);
//Methods can be added and called.
}
removeHobby(hobby) {
//Find the index of the parameter.
let foundIdx = this.hobbies.indexOf(hobby);
//Collect all values in front of the parameter hobby.
let valuesBefore = this.hobbies.slice(0, foundIdx);
//Collect all values after. foundIdx + 1 means to go to the end of the array.
let valuesAfter = this.hobbies.slice(foundIdx + 1);
//Update hobbies. Concat to add them with the parameter hobby removed.
this.hobbies = valuesBefore.concat(valuesAfter);
}
greeting() {
console.log("Hello, how are you today?");
}
}
let Seth = new Person("Seth", "Misty", "New Orleans", "Video Games");
//Filling out the constructor.
console.log(Seth);
// Exercise 2 Section
console.log("EXERCISE 2:\n==========\n");
class Coder extends Person {
//Extend adds new class as a subtype of the previous.
constructor(name, pets, residence, hobbies, occupation) {
//New constructor because this is a new class.
super(name, pets, residence, hobbies);
//Super add the properties from the Parent class.
this.occupation = occupation;
//Assigning new property.
}
greeting() {
console.log("Hello, I'm learning prototypes.");
}
}
// Exercise 3 Section
console.log("EXERCISE 3:\n==========\n");
let person = new Person("Seth", 1, "New Orleans", [
//Using prototype to make a new Person.
"Gaming",
"Japanese",
"Drawing",
]);
console.log(person);
let coder = new Coder(
//Using prototype to make a new Coder
"Seth",
0,
"New Orleans",
["Gaming", "Japanese", "Drawing"],
"Full Stack Web Developer"
);
coder.addHobby("Cooking");
console.log(coder);
// Exercise 4 Section
console.log("EXERCISE 4:\n==========\n");
class Calculator {
constructor() {
//Constructor can be empty for this property to be placed.
this.result = 0;
}
add(num1, num2) {
this.result = num1 + num2;
return this.result;
}
subtract(num1, num2) {
this.result = num1 - num2;
return this.result;
}
multiply(num1, num2) {
this.result = num1 * num2;
return this.result;
}
divide(num1, num2) {
this.result = num1 / num2;
return this.result;
}
displayResult() {
console.log(this.result);
}
}
let calc = new Calculator();
calc.add(1, 2);
calc.displayResult();
calc.subtract(1, 2);
calc.displayResult();
calc.multiply(1, 2);
calc.displayResult();
calc.divide(1, 2);
calc.displayResult();