A little personal view of JS's call () and apply () two functions

Source: Internet
Author: User


First of all, call () and apply are JS's built-in functions

It does this by changing the pointer to the "first parameter object" in the call or the Apply function so that it turns to the function that references it

Call () usage, call (object, parameter 1, parameter 2,)

Use of apply (), apply (object, array parameter)

The difference between call () and apply () is that the argument following the call object is a string, and the argument following the Apply object is an array

In other words: Change the This of the first parameter object to point to the function referencing it

Example:
function Func.call (OBJ,ARG2,ARG3)
The meaning of this example is:
Call puts the first parameter, the this of the Obj object, to the this of Func and passes the parameter to the Func function arg1,arg2

For the first parameter, obj, here's a second explanation:
"OBJ: This is an object, it can be a function, and if it is a function, the call method will eventually parse to" the object that makes up the function ", which is also said to be the context of the function"
Let's do the experiment here:

 //building the Add function of a global object    functionAdd (A,b) {Alert (a+b); }    varobj={        }; //build the Add function with obj, note that the function body here is: a*b, to indicate the difference from the above addObj.add=functionAb) {Alert (a*b); } obj. sub=functionAb) {Alert (a/b); }    //Building a sub function    functionSub (A,b) {Alert (a-b); } obj. arg1=function() {alert (a%b); } obj. arg2=functionAC) {Alert (a%b); }    functionarg () {}//Experiment 1Obj.add.call (add,1,2);//The result is: 2Obj.add.call (window.add,1,2);//The result is: 2Obj.add.call (window,1,2);//The result is: 2//Experiment 2Add.call (obj.add,1,2);//The result is: 3Add.call (obj,1,2);//result is 3Add.call (window,1,2);//The result is 3//Experiment 3Sub.call (add,1,2);//result is-1Sub.call (window.add,1,2);//result is-1Sub.call (obj.add,1,2);//The result is-1//Experiment 4Add.call (sub,1,2);//result is 3Add.call (window.sub,1,2);//result is 3Add.call (obj.sub,1,2);//The result is 3//experiment 6Obj.call (sub,1,2); //uncaught TypeError:obj.call is not a function (anonymous function)Window.call (sub,1,2); //uncaught TypeError:window.call is not a function (anonymous function)//Experiment 7Obj.arg1.call (add,1,2); //uncaught referenceerror:a is not definedObj.arg2.call (arg,1,2);//Results: 1

Experiment 1, 2, 3, 4, 5, 7 as Control:

It is concluded that the first parameter of the call method is the object, and the rest of the arguments are passed in the value inside the ' object ' method.

Experiment 6: Come to the conclusion that call is just a method of ' function ', reference it must be ' function ', ' object ' reference it will error

Post-knot language:

If my conclusions above are correct, then the following functions and results should be executed:

Obj.sub.call (sub,1,2);//Results: 0.5
Obj.sub.call (window.sub,1,2);//Results: 0.5
Obj.sub.call (window,1,2);//Results: 0.5

You can do an experiment to verify my conclusions.

A little personal view of the call () and apply () two functions of JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.