This article mainly summarizes the usage of call and apply in js. If you need a friend, please refer to it and hope to help you with the interview the day before yesterday, some gg asked some js knowledge, including a call and apply usage question. Although I used the call method 365 days ago, I still could not answer it. I will give a further summary today.
Call and apply. They are used to bind a function to another object and run the function.
The format and parameter definitions of the two are as follows:
Call (thisArg [, arg1, arg2 ,... ]); // Parameter list, arg1, arg2 ,...
Apply (thisArg [, argArray]); // parameter array, argArray
The this pointer inside the above two functions will be assigned to thisArg, which can be used to run the function as a method of another object.
I. Simple call usage
First, let's take a look at a simple example (call ):
The Code is as follows:
Call-apply