The main point of call
is to set the value of this
within the function. Since your doStuff
doesn't use this
within the function, using call
with it is pointless.
Here's an example where it matters:
function doStuff(a, b) {
return this.sum(a, b);
}
var obj = {
sum: function(a, b) {
return a + b;
}
};
console.log(doStuff.call(obj, 3, 4)); // 7, because `obj` has a `sum` property
console.log(doStuff(3, 4)); // Fails with an error that `this.sum` is not a function
so why doesn't the below work? (result => NaN)
doStuff.call(myContext,this.c,this.d)
Because this.c
is evaluated before the call to doStuff
, using whatever the current this
value is. How you're probably calling that code, this
is (in loose mode) the global object (the window object, on browsers), which probably doesn't have a c
or d
property. (In strict mode, again making assumptions about how you're calling that, you'd get an exception, because this
is undefined
and you can't retrieve properties from undefined
.)