Reputation: 706
From this question, given that I don't want to specify any context, hence, passing null
to thisArg
in call()
.
What would be the difference between line 2 and line 3 in the following code? Is there any benefit from doing one over the other?
function sum(a,b) { return a + b; }
var result1 = sum.call(null,3,4); // 7
var result2 = sum(3,4); // 7
Similarly for apply()
:
var arr = [1,2,4];
var result3 = Math.max.apply(null, arr); // 4
var result4 = Math.max(...arr); // 4
Upvotes: 0
Views: 60
Reputation: 1075219
It depends on whether the function you're calling was defined in loose mode or strict mode.
When calling a loose-mode function, the following three things all call the function such that this
within the call is the global object for the environment (globalThis
, aka window
on browsers):
this
: fn()
this
value of undefined
: fn.call(undefined)
and similarthis
value of null
: fn.call(null)
and similarWith a strict mode function, the first two both cause this
during the call to be undefined
, and the third (explicitly setting it to null
) sets it to (you guessed it) null
.
Examples:
function loose() {
console.log(`loose: ${this === null ? "null" : typeof this}`);
}
loose();
loose.call(undefined);
loose.call(null);
function strict() {
"use strict";
console.log(`strict: ${this === null ? "null" : typeof this}`);
}
strict();
strict.call(undefined);
strict.call(null);
In the normal case, if you don't need to set this
to anything in particular, just call the function so the default behavior takes place.
fn(...theArray)
. If you're stuck in an obsolete environment, the rough equivalent is fn.apply(undefined, theArray)
.If you have a specific need to set a specific this
value, you can do that via call
or apply
(as you've found), including (for strict mode functions) undefined
or null
.
Upvotes: 2
Reputation: 55678
TJ Crowder has the detailed response here, but as a general rule:
fn.call(null)
: In almost all cases, just call the function.fn.apply(null, args)
: This is useful in some cases where you have an array of arguments and your environment doesn't support ...args
, but otherwise spreading the arguments is probably more conventional: fn(...args)
Upvotes: 2