Interview questions often test --js & compulsory in the call () and apply ()

apply:

Accepts two parameters, the first parameter is the value of this was bound to, the second parameter is a parameter array . When the first argument is null, undefined, and pointing to the default window.

call:

The first parameter is the value of this to bind to, the incoming followed by a list of parameters . When the first argument is null, undefined, and pointing to the default window.

apply and call:

In fact apply and call usage is almost the same. The only difference is that: When the function need to pass a plurality of variables, apply an array as a parameter can accept input, call it is receiving a series of individual variables.

 

1. First, a familiar look:

 

 

2. Secondly:

 

 

 

Output looks like:

 

 

 

 

 

Analysis: When using the call () function, bigcar car constructor which uses the attribute. It changed the point of this

 

3. Thirdly

 

 

 

 result:

 

Analysis: The role of apply () and call () is the same, apply with projects in developing some more.

4. I added a few things:

 

Output:

 

 

 5. Finally, to a case of it

 

Output:

 

Guess you like

Origin www.cnblogs.com/snowbxb/p/11589796.html