0

Can you help me with why do we pass 'this' as an parameter on the User.call()?

function Admin(name, age, location) {
    User.call(this, name, age);
    this.location = location;
}
Heretic Monkey
  • 10,498
  • 6
  • 45
  • 102
  • `User.call(this, name, age)` - this is a call to `User` constructor function to initialize `name` and `age`. `.call()` method is used to explicitly set value of `this` inside the `User` constructor function to the newly created object inside `Admin` constructor function. As a result `this.name = name;` inside `User` constructor function will add a new property named `name` on the newly created object inside `Admin` constructor function – Yousaf Jul 28 '20 at 14:58
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call – Ry- Jul 28 '20 at 14:58
  • Does this answer your question? [How do I pass the this context to a function?](https://stackoverflow.com/questions/3630054/how-do-i-pass-the-this-context-to-a-function) While the questions may differ, the answers to the linked question answer this question as well. – Heretic Monkey Jul 28 '20 at 15:02

0 Answers0