Skip to content

Instantly share code, notes, and snippets.

@myogeshchavan97
Created April 14, 2020 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myogeshchavan97/2cdbad8abf0a2e2810fb0e2102dff77d to your computer and use it in GitHub Desktop.
Save myogeshchavan97/2cdbad8abf0a2e2810fb0e2102dff77d to your computer and use it in GitHub Desktop.
Prototypal Inheritance
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.display = function () {
console.log(this.name, this.age);
};
function Employee(name, age, salary) {
Person.call(this, name, age);
this.salary = salary;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
const emp = new Employee('Mike', 20, 4000);
console.log(emp); // { name: 'Mike', age: 20, salary: 4000 }
emp.display(); // Mike 20
console.log(emp.constructor); // Employee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment