For example, we have a function
function FirstFunction(name, surname){
this.name = name;
this.surname = surname;
...
}
We have some functions in its prototype and we have another function "SecondFunction" with it's own prototype. When I want to inherit prototypes I write
SecondFunction.prototype = Object.create(FirstFunction.prototype);
Now, when I try to create new variable with
var newVariable = new SecondFunction();
I want to pass arguments 'name' and 'surname' that are listed in the FirstFunction to be able to use functions in the FirstFunction's prototype. Which is the best way to do that?
I think the right way to do it is using call
or apply
:
function FirstFunction(name, surname){
this.name = name;
this.surname = surname;
}
function SecondFunction(name, surname) {
FirstFunction.call(this, name, surname)
}
SecondFunction.prototype = Object.create(FirstFunction.prototype);
var newVariable = new SecondFunction('Harry', 'Potter');
console.log(newVariable);
You can refer to this article that explains it.