Analysis of the apply () method in Javascript, javascriptapply

Source: Internet
Author: User

Analysis of the apply () method in Javascript, javascriptapply

We have mentioned the Javascript Call method before. This time we will talk about the apply method similar to the Call method.

Apply vs call

The difference between the two is: whether the parameter is passed or the parameter Array

This is the call usage.
Copy codeThe Code is as follows:
TheFunction. call (valueForThis, arg1, arg2 ,...)

This is apply.
Copy codeThe Code is as follows:
TheFunction. apply (valueForThis, arrayOfArgs)

Therefore
Copy codeThe Code is as follows:
ArrayOfArgs = [arg1, arg2,...];

Javascript apply Method

First, let's take a look at the usage of the previous call.
Copy codeThe Code is as follows:
Function print (p1, p2 ){
Console. log (p1 + ''+ p2 );
}
Print. call (undefined, "Hello", "World ");

From the above description, we can conclude that when
Copy codeThe Code is 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 result 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.