SoFunction
Updated on 2025-03-10

Combined usage analysis example

Yesterday I saw a very interesting js interview question on the Internet, so I discussed it with my colleagues. I found that it was very confused at the beginning and finally realized it. After I understood it, I found that it was quite simple. I will share it with you!
The title is as follows: var a = (function(a){return a;}, [0,4,3]);alert(a);

The analysis steps are as follows:
1. Treat it as a whole. The call method is a local method implemented by the browser and an internal method of the function type.
var a = ().apply(function(a){return a;}, [0,4,3]);

2. (obj,args) is equivalent to (args). This step is the key point and must be understood!
(function(a){return a;}).apply(0,[4,3])
(function(a){return a;}).call(0,4,3)

3. The result is very obvious at this point. a is 4, and the result of alert is 4.

The confusing point of this question is that if you understand it well, you will be clear and clear!