JavaScript methods--call and apply

Source: Internet
Author: User

1, the same point:

A) The effect or effect is exactly the same;

b) at least one parameter;

c) The first parameter must have an object, because this guy is lazy.

2, different points: the way to pass the parameters.

Premise:

1. There are two objects: A and B;

2, B There is a method (here with function instead);

The method (Function) of 3 and B has two kinds of parameters, but the method produces the same result.

One is in addition to the first parameter, the remaining parameters are packaged into an array, that is, b.function.apply (A,args);

The other is that all parameters exist separately (including the first parameter) in their own form, i.e. B.function.call (A,arg,arg,......) ;

4, a want to have and b the same method, but do not want to create their own, that is, just want to borrow someone else ready-made things (good cheat).

Specific Description:

1, B.function.call (A,ARG,ARG), this example is about the "a" object "call" B object Function (a specific method).

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 (I have not tried).

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 call 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 (if there can be more than one, you need to confirm).

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

The above east can have several places I have not tried, for example, said:

1. "At least one parameter", what if there are no parameters?

2, "the first parameter must have and is an object, because this guy is lazy." "And what if the first argument is empty?"

3. "Arg can be any type, including array", whether Arg can really be any type, I am passing arguments of type string.

JavaScript methods--call and apply

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.