I’m studying apply and I am trying to understand why the code I am studying only passes one parameter to apply.
I first define Quo:
var Quo = function(string) {
this.status = string;
};
Next I define get_status:
Quo.prototype.get_status = function() {
return this.status;
};
I define statusObject:
var statusObject = {
status: 'yo!'
};
And this is where I am lost:
var status = Quo.prototype.get_status.apply(statusObject);
// status is 'yo!'
According to the documentation “Apply Calls a function with a given this value and arguments provided as an array.” You can see in the case, using apply I pass only a single parameter, which I believe is defining “this”. Can you clear up what exactly is happening in this method, why apply is necessary, and why in this case I can only pass one param to the method, when it states two are needed. Thank you.
applysets the context of the function being applied to the object provided in the first parameter.If an array is passed as the second parameter, the parameters will be set based off the values in the array:
The second parameter in
applyis optional, howevercallis typically used for settings context: