vertti vertti - 2 months ago 11
Javascript Question

Nested call to function in Javascript

Intellij IDEA shows a warning when ever I write Javascript like this:

someFunction(someOtherFunction());


But the explanation is not really helpful:


"This inspection reports any Javascript function calls used as
arguments to another function call."


This is something I do frequently, so what's the potential trap hiding there worth warning of? Or if it's just some coding convention, what's the reason for it?

Answer

It is a warning because most of the time, you want to pass a function reference as an argument. It is mostly used as a callback:

someFunction(someOtherFunction);
someFunction(fn){
    fn.call();
}

In that example, someOtherFunction() instead of someOtherFunction would not work as expect (unless someOtherFunction returns a function itself).

someFunction(someOtherFunction()); work more like a getter.

someFunction(someOtherFunction());
someFunction(int){
    alert(int === 1);//True;
}
someFunction(){
    return 1;
}

It gives a warning because it is a common mistake for new developers.