0

Person is base class and Emp inherits from Person. I am trying to use name, location properties of Person in Emp.

function Person(name, location){
   this.name = name;
   this.location = location;
}

Person.prototype.getName = function(){
   return 'Name: ' + this.name;
}

function Emp(id, name, location){
     this.id = id;
     Person.call(this, name);
     Person.call(this, location);
}

Emp.prototype = Object.create(Person.prototype);

var e1 = new Emp(1, 'John', 'London');
e1.id   // 1
e1.name // 'London'
e1.location //undefined

What is causing this error and why is name taking value of London?

1 Answer 1

3

Why are you calling the constructor twice with a single argument?

function Emp(id, name, location){
     this.id = id;
     Person.call(this, name, location);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.