JavaScript apply and call have an in-depth understanding

is mainly a bit of a problem with code execution, so I won"t list the use of dealing with apply and call here. The code is as follows. I don"t understand it from Code 2. I don"t understand even if I look at the ECMAScript standard. I hope I can get a great answer

. < H1 > Code 1 < / H1 >
"function" // console
Uncaught TypeError: test is not a function // test();  chrome
TypeError: undefined is not a function // test(); safari
Apr.16,2021

first of all, simplify the 1 split
Function.call.apply (function (a) {return a;}, [0JI 4J 3]);
from back to front apply changes this in Function.call to function (a) {return a;} and gives it 3 parameters
Function.call .

function (a) {return a;} .call

the last step is to call the function (a) {return a;} method whose this is 0 has two parameters 4 code 3 , so it outputs 4

.

bring the following questions to the 1 question

2. {} .call () , {} has no call method
3. window.call () , apply the first parameter is undefined,null non-strict mode is automatically bound to window , not passed as undefined , window does not have call method
4. Function.call () , Function is a constructor the sentence call is empty this or window is Function ()
5. window.call () , just like 3 , the scope within call becomes window

.

Code-2:
you treat Function.call as a function. If the apply method is called on this function, then the first parameter is the this object, and the this object required by Function.call must be a funtion, that you pass {}, which is an empty object, so you have to do a type conversion, that is, converting from {} to a function, then it will definitely report a type conversion error, so the error report of safari is the most stringent.

Menu