Saturday, 15 April 2017

Conditions in class constructor javascript

I have this code

class Person{
  constructor(person){
    this._name=person._name;
    this._age=person._age;
  }
}

class Employee extends Person{
  constructor(person){
    if(person instanceof Person){
    super(person);
    }
  }
}

let emp=new Employee({_name:'Uday',_age:24});
console.log(emp);

I want to call super(person) only when I get valid person object. I'm getting this error this hasn't been initialised - super() hasn't been called. How to make sure I pass only valid person object to super class?



via Uday Vishwakarma

No comments:

Post a Comment