I saw a very interesting js interview question on the internet yesterday and discussed it with my colleagues. I found it quite simple at the beginning, and I will share it with you!
The question is as follows: var a = Function. prototype. call. apply (function (a) {return a ;}, [, 3]); alert ();
The analysis procedure is as follows:
1. Use Function. prototype. call as a whole. The call method is a local method implemented by the browser and an internal method of the Function type.
Var a = (Function. prototype. call). apply (function (a) {return a ;}, [0, 4, 3]);
2. fun. apply (obj, args) is equivalent to obj. fun (args). This step is important and must be understood!
(Function (a) {return a;}). apply (0, [4, 3])
(Function (a) {return a;}). call (0, 4, 3)
3. The result in this step is obvious. a is 4, and alert is 4.
The confusion of this question lies in Function. prototype. call. After understanding it, it is clear!