Skip to content

Instantly share code, notes, and snippets.

@quangkeu95
Created November 6, 2018 07:53
Show Gist options
  • Save quangkeu95/d81a9cd4f1dbf0abc19491cab7409a7c to your computer and use it in GitHub Desktop.
Save quangkeu95/d81a9cd4f1dbf0abc19491cab7409a7c to your computer and use it in GitHub Desktop.
Prototype-based Inheritance Class
function Person(name, age) {
this.name = name;
this.age = age;
};
Person.prototype.sayHi = function() {
console.log(`${this.name}, ${this.age} say hi`);
};
function Employee(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
};
Employee.prototype.work = function() {
console.log(`${this.name} is ${this.job}`);
};
// Setup the inheritance chain
Employee.prototype.__proto__ = Person.prototype;
let employee = new Employee("Chris", 24, "developer");
employee.sayHi(); // Employee can say hi
employee.work();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment