Skip to content

Commit

Permalink
Learn inheriting from a class
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Nguyen committed Feb 10, 2022
1 parent 3be65bb commit 9e4b5cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 8-using-inheritance/inheritance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
toString() {
return `Name: ${this.firstName} ${this.lastName}`;
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
get lastName() {
return this._lastName;
}
set lastName(value) {
this._lastName = value;
}
}

class ReputablePerson extends Person {
constructor(firstName, lastName, rating) {
super(firstName, lastName);
this.rating = rating;
}
toString() {
return `${super.toString()} Rating: ${this.rating}`;
}
get fullName() {
return `Reputed ${this.lastName}, ${super.fullName}`;
}
}

const alan = new ReputablePerson("Alan", "Turing", 5);
console.log(alan.toString());
console.log(alan.fullName);
5 changes: 5 additions & 0 deletions 8-using-inheritance/using-inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ JavaScript implements **prototypal** inheritance
## Behavior of Get vs. Set

- Gets search deep, but sets are always shallow

## Inheriting from a Class

- Extending a class
- Overriding methods

0 comments on commit 9e4b5cc

Please sign in to comment.