I read this snippet in the definitive guide:
function not(f)
{
return function()
{
var result=f.apply(this,arguments);
return !result;
}
}
What I can’t understand is, since this function f is in the closure, it’s this is already this, why wouldn’t this snippet just directly use var result=f(arguments);?
I even read some calls with undefined/null as the first parameter which I think can completely be replaced with direct call:
...
while(i>len)
{
if(i in a)
accumulator=f.call(undefined,accumulator,a[i],i,a);
i++;
}
...
Why did the author use call() but not direct call? are there any difference between direct function call and call() with undefined as it’s first parameter?
…Will call
f()passing a single argument, theargumentsobject.…Will call
f()passing the arguments in theargumentsobject individually (so to speak).So let’s say
f()was defined as:Then given three arguments
1,2,3the direct call withargumentsis like this:(Note that
argumentsis array-like; it isn’t an actual array.)Whereas the
.apply()version is like this: