What is the purpose of calling (0, func) () in JS?

I stumbled upon this in the code generated by Babel from this source . It seems that it somehow protects the required function.

(0, _utilities.validateNextState)(nextDomainState, reducerName, action); 

I understand how the comma operator in parentheses drops 0 and returns a validateNextState function, but why not just do:

 _utilities.validateNextState(nextDomainState, reducerName, action); 

My guess is the type of guard (for example, closing the guard area or setTimeout makes an asynchronous function call), but cannot understand what the target is.

+5
source share
1 answer

Sometimes the cryptic semantics of JavaScript are probably the cause. Expression

 (0, _utilities.validateNextState) 

Evaluates, of course, the link to this function. However, since this subexpression in parentheses indicates a function call that is outside this, without the _utilities object _utilities recognized as the context for the call ( this value). Thus, inside the validateNextState this function will be either undefined or a reference to a global object, depending on the state of the "strict" mode.

I suspect Babel is doing this because in the source code, the call to validateNextState() is executed as if it were a bare function, not an object method. Babylon does not know (possibly) whether this value is relevant for this particular function, but it must make sure that it is safely called.

+3
source

Source: https://habr.com/ru/post/1273225/


All Articles