The difference between call and apply method in JS

Source: Internet
Author: User

1. Call method

First Little Demo:

<span style= "FONT-SIZE:14PX;" >function Add (A, b) {alert (a+b),} function sub (a, b) {alert (A-a);} <span style= "color: #ff6600;" >add</span>.call (sub,2,1); </span>

I believe that after this demo, we all understand that the Add () method is used to replace the sub ()Method, Add.call (sub,3,1) = = Add (3,1), so the result is: Alert (3)


2. Apply method

The Apply method is actually the same as the call method , only slightly different on the parameter: (that is , the second argument passed in by the Apply method must be an array of arguments , That is, if the Apply method is more than two arguments, then all parameters after the second parameter must be passed in as an array , and the first parameter remains the same as the call method )

The obj.call (ARG,AAA,BBB,CCC) method corresponds to the Apply notation:obj. Apply (arg, [AAA,BBB,CCC]) , the effect of both is the same.

The difference between call and apply method 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.