I'm just trying out function.apply() in javascript, I'm having trouble understanding how it parses the array parameter we pass in. For example,
var update = function(name, profession){
this.name = name;
this.profession = profession;
}
var p1 = {
name : "bond",
profession : "actor"
}
console.log(p1.name, p1.profession); //outputs bond actor
Now lets change p1's properties using apply() method
update.apply(p1, ["john", "developer"]);
1st argument is value for this pointer, and 2nd argument is an array
console.log(p1.name, p1.profession); //outputs john developer
The function update() takes in two parameters name and profession, but I'm only passing in one array parameter ["john", "developer"] to it through apply() method. I do not understand how my update() method is correctly capturing the properties from the array that is passed in and updating each property accordingly.
Thank you!
thisas you said in its entirety. The second array will be expanded so that each argument in the array becomes a separate function parameter when the function is called.applydoes that? It's exactly whatapplywas meant to do, and you seem to know that it does it, so what's the problem?updatemethod setting the context properties as though it was some form of constructor. Both approaches are not needed at the same time. They have the same affect either way.