What are the different records of call and apply in JS?

Source: Internet
Author: User

First, call and apply the difference

The way the parameters are passed. The use of different, mainly is the parameter is not exactly the same

(1), B.function.call (A,arg,arg)

This example is about the function (a specific method) of a object's "invoke" B object.

Note the parameter arg in the call method:

A) The number of ARG is zero or more;

b) Arg can be of any type, including array.

Call is translated here as "invocation", which can be understood in JS as "a method of invoking the B object by a object";

(2), b.function.apply (A,args)

This example is about the function (a specific method) of a object's "apply" B object.

Note the parameter args in the Apply method:

A) args is an instance of the array object, which is an array;

b) The number of args is 0 (that is, it does not work) or 1;.

Apply is translated here as "applied", that is, "a object that applies a method of a B object."

Second, call and apply examples

functionPerson (name,age) { This. name=name;  This. age=Age ;  This. Money = 500;} functionMyfun (Money) {alert ( This. Money);}varMoney = 100; myfun (money);//return 100; Window.myfun (Money); Save to Myfun (money);//this time this point is the Window object, in fact the myfun function and the definition of var money = 100; all as a Window object sub-object (that is, a global object)myfun.apply (window,[]); //Save to Window.myFun.apply (window,[]); //return 100; Ibid .myfun.apply (NewPerson (' Zhangsan ', 23),[]); //return 500, an empty array as a parameter, only meets the syntax requirements//this time the Myfun method inside this point is the new person (' Zhangsan ', 23) object, two is not Myfun class (function), so pop upMyfun.call (NewPerson (' Zhangsan ', money,300, ' Mycardid ')); //return 500, followed by money,300 and Mycardid are parameter lists as parameters, listed//this time the Myfun method inside this point is the new person (' Zhangsan ', 23) object, two is not Myfun class (function), so pop up

From: http://blog.csdn.net/whaxrl/article/details/25116809

What are the different records of call and apply in 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.