Saturday, 20 October 2018

JavaScript: Inherit from an object

You can inherit from an object using ‘Object.setPrototypeOf()’ method.

HelloWorld.js
var organization = {
  orgName : "ABC Corporation",
  location : "New Delhi"
}

class Employee{
  
  constructor(firstName, lastName){
    this.firstName = firstName;
    this.lastName = lastName;
  }
  
}

Object.setPrototypeOf(Employee.prototype, organization);

var emp1 = new Employee("Ashwani", "Kumar");

console.log(emp1);


Output



Previous                                                 Next                                                 Home

No comments:

Post a Comment