Application () method in JavaScript _javascript skills

Source: Internet
Author: User

We talked about the Javascript call method, and this time we're going to say the Apply method like say and called.

Apply vs Call

The difference between the two is whether a parameter is passed, or an array of arguments

This is the use of call.

Copy Code code as follows:

Thefunction.call (Valueforthis, arg1, arg2, ...)

And this is the Apply
Copy Code code as follows:

Thefunction.apply (Valueforthis, Arrayofargs)

So
Copy Code code as follows:

Arrayofargs = [Arg1, arg2, ...];

Javascript Apply Method

Let's take a look at the previous call usage.

Copy Code code as follows:

function Print (p1, p2) {
Console.log (p1 + ' + p2 ');
}
Print.call (Undefined, "Hello", "World");

By the above narrative, we can conclude that when
Copy Code code as follows:

args = "Hello", "World";
function Print (p1, p2) {
Console.log (p1 + ' + p2 ');
}
Print.call (undefined, args);

The two are equivalent, but they are also equivalent, the output is also "Hello,world"!

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.