Created
April 14, 2020 10:42
-
-
Save myogeshchavan97/2cdbad8abf0a2e2810fb0e2102dff77d to your computer and use it in GitHub Desktop.
Prototypal Inheritance
This file contains 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
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