Briefly, this apply
will execute the parent class constructor in the context of the daughter class. That’s what it does, calls a function forcing a value to this
within it. That is, any reference to this
in the builder ClassePai
will be referring to the this
which has just been created in the execution of new ClasseFilha(...)
.
You said:
I get confused like the ClassePai.apply()
modify the ClasseFilha
without a direct assignment on a variable
Actually you’re not modifying ClasseFilha
(which is a constructor), rather an object that is being instantiated with new ClasseFilha()
. And not necessarily you are modifying, it depends on what the builder ClassePai
makes. If you are familiar with OOP, the line of apply
is equivalent to a call from super()
in languages that implement class-based OOP (JS is based in prototypes).
You commented:
How apply modifies this?
The this
in itself (the reference) is not modified. The object to which it points - which has just been created with new ClasseFilha()
- can be changed by the function ClassePai
. This will depend on the code of this function. And it will depend on the context where the code snippet you posted is inserted. Which leads to your next question:
How does this in the context of the daughter class inherit the parent class methods after the passage of line two? This is one of the characteristics of apply?
This kind of construction usually comes in a context like this:
var ClassePai = function ClassePai(nome) {
this.nome = nome;
};
var ClasseFilha = function ClasseFilha (){
ClassePai.apply(this, arguments);
};
var filho = new ClasseFilha('nome do filho');
In this case, the object filho
will win an estate nome
which is "injected" into it by the father builder. The call of the apply
only guarantees the execution of the superclass constructor. Its methods are not available. If the constructor ClassePai
Trying to execute a method that is part of your prototype will be a mistake, because in that code the daughter class does not inherit the prototype from the father. But it is possible to inherit, so:
var ClassePai = function ClassePai(nome) {
this.nome = nome;
this.ola();
};
ClassePai.prototype = {
ola: function() {
console.log('Olá, meu nome é ' + this.nome);
}
};
var ClasseFilha = function ClasseFilha (){
ClassePai.apply(this, arguments);
};
// Sem a linha abaixo, o construtor do pai
// daria erro na chamada a this.ola.
// Esta linha é que arma a herança entre as classes
// (coloca as duas na mesma cadeia de protótipos)
ClasseFilha.prototype = Object.create(ClassePai.prototype);
var filho = new ClasseFilha('nome do filho');
On the question, if you have familiarity with OOP, it amounts to a call
super()
.– bfavaretto
Yes, that I understand. I don’t understand how the call works within the JS engine logic. How does apply modify this? How does this in the context of the daughter class inherit the parent class methods after the passage of line two? This is one of the characteristics of apply?
– Brunno Vianna
bfavaretto explained even better than me. If you have doubts ask/comments. If it was solved I find his answer more complete.
– Sergio