Skip to content

Commit 9890c75

Browse files
author
Max Kelly
committed
100. Creating SubClasses
1 parent 6759365 commit 9890c75

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

oop/person.js

+61-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Prototypal Inheritance
2+
// myPerson --> Person.ptototype --> Object.prototype --> null
23

34
//---- Class Syntax -----
45
// What makes the person class is the new way to create a constructor function. You define the constructor inside the class instead of inside the function.
@@ -25,13 +26,67 @@ class Person {
2526
setName(fullName) {
2627
const names = fullName.split(" "); // This .split allows us to split a string and make an array. We're currently splitting on a space. This would create just Andrew
2728
this.firstName = names[0];
28-
this.lastName = [1];
29+
this.lastName = names[1];
2930
}
3031
}
32+
33+
// The class Employee extends from Peron meaning we it has the same behaviour as person. This means we don't have to duplicate code.
34+
class Employee extends Person {
35+
constructor (firstName, lastName, age, position, likes) {
36+
super(firstName, lastName, age, likes) // Super allows you to call the functions in the Person constructor.
37+
this.position = position
38+
}
39+
getBio() {
40+
// Max is a Helicopter Pilot
41+
return `${this.firstName} ${this.lastName} is a ${this.position}.`
42+
}
43+
getYearsLeft() {
44+
return 65 - this.age
45+
}
46+
}
47+
48+
class Student extends Person {
49+
constructor(firstName, lastName, age, grade, classes, likes) {
50+
super(firstName, lastName, age, likes);
51+
this.grade = grade;
52+
this.classes = classes;
53+
}
54+
55+
updateGrades(number) {
56+
this.grade += number
57+
}
58+
59+
getBio() {
60+
let status = `${this.firstName}`;
61+
if (this.grade >= 70) {
62+
status = `${this.firstName} is passing the test! Hooray! `;
63+
} else {
64+
status = `${this.firstName} is failing the test :(`;
65+
}
66+
return status;
67+
}
68+
}
69+
70+
71+
// 1. Create a class for students
72+
// 2. Track student grade 0 - 100
73+
// 3. Override bio to print a passing or failing message. 70 and above "Andrew is passing the test"
74+
// 4. Create "UpdateGrade" that takes the amount to add or remove from the grade.
75+
76+
// Create student
77+
// Print status
78+
// Change grade to change status.
79+
80+
const me = new Student('Max', 'Kelly', 21, 10, 'English')
81+
console.log(me.getBio())
82+
me.updateGrades(+80);
83+
console.log(me.getBio())
84+
3185
// ---------------------
3286
// The below creates a new person and logs it to the console.
33-
const me = new Person('Max', 'Kelly', 21, ['surfing', 'SnowBoarding'])
34-
me.setName('Andrew Smith')
35-
console.log(me.getBio())
36-
const person2 = new Person('John', 'Boob', 29)
37-
console.log(person2.getBio())
87+
// const me = new Employee('Max', 'Kelly', 21, 'Helicopter Pilot',['surfing', 'SnowBoarding'])
88+
// me.setName('Andrew Smith')
89+
// console.log(me.getBio())
90+
// console.log(me.getYearsLeft())
91+
// const person2 = new Person('John', 'Boob', 29)
92+
// console.log(person2.getBio())

0 commit comments

Comments
 (0)