Last active
April 11, 2025 16:32
-
-
Save prof3ssorSt3v3/c056b8b5f379ee2767bb4e8ad90f3dac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creating objects with Classes | |
* Versus objects with prototypes | |
* Since JavaScript is not a Class-based language | |
* what is happening behind the class syntax? | |
*/ | |
let PersonC = class { | |
constructor(nm, id) { | |
this.name = nm; | |
this.id = id; | |
} | |
getDetails() { | |
return `${this.name} :: ${this.id}`; | |
} | |
}; | |
let bob = new PersonC("Bob", 123); | |
console.log(bob.getDetails(), bob.name); | |
let EmployeeC = class extends PersonC { | |
// EmployeeC prototype links to PersonC prototype | |
constructor(nm, id, salary) { | |
super(nm, id); | |
this.salary = salary; | |
} | |
employeeInfo() { | |
//exist on the prototype of EmployeeC | |
return `${this.name} :: ${this.id} :: ${this.salary}`; | |
} | |
}; | |
let noomi = new EmployeeC("Noomi", 456, 8500000); | |
console.log(noomi.employeeInfo()); | |
/////////////////////////////////////////////// | |
let PersonP = function(nm, id) { | |
this.name = nm; | |
this.id = id; | |
}; | |
PersonP.prototype.getDetails = function() { | |
return `${this.name} :: ${this.id}`; | |
}; | |
let fred = new PersonP("Fred", 321); | |
console.log(fred.getDetails(), fred.name); | |
let EmployeeP = function(nm, id, salary) { | |
PersonP.call(this, nm, id); | |
this.salary = salary; | |
}; | |
Object.setPrototypeOf(EmployeeP.prototype, PersonP.prototype); //extends NOTE: THIS LINE WAS CHANGED | |
EmployeeP.prototype.employeeInfo = function() { | |
return `${this.name} :: ${this.id} :: ${this.salary}`; | |
}; | |
let mary = new EmployeeP("Mary", 654, 65000); | |
console.log(mary.employeeInfo()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think we also need to add Object.setPrototypeOf(EmployeeP, PersonP) to address static class method prototype inheritance