Hi,
I was just trying to clear my concept on apply and call reading some online content and watcing some videos, when i decided to try it myself and write some code , i wrote -
function addNum(num1, num2) {
return num1 + num2;
}
var o = {
a1: 23,
b1:26
}
addNum.apply(o);
output ----> NaN
Now i tried with out looking for solution and struggled to understand why NaN is output, as i am now applying the logic of addNum and saying take the variables from context of o…
Still i got error, then when i finally looked up i found i needed to do this …
function addNum(num1, num2) {
return this.num + this.num2;
}
var o = {
num1: 21,
num2: 12
}
addNum.apply(o);
Now it works …
is this not highly prohibitive, i mean first of all why i need to write “this” and then why the names of variables need to be same inside object and function…
Please tell me the reason why i need to do this …
NOTE - i just read that Douglas Crockford is highly against using "this’, so can apply and bind and call be done with out “this” as well ?
Kindly help and guide in this regard …