The role and difference of call () and apply ()

Source: Internet
Author: User

   Call, Apply is a method of Function.prototype, which is implemented internally by the JavaScript engine because it belongs to Function.prototype, so each function object instance, that is, each method has a call , apply property. As a property of the method, the use of them is of course a method. These two methods are easy to confuse, because they work the same way, but only in different ways.

The same thing: they all have the same effect.

Different points: Their parameters are different

Call (THIS,ARG1,ARG2,ARG3);

Apply (this,arguments);

Apply has the same effect as call.

Call, the role of apply is to borrow someone else's way to invoke, just like calling their own.

function Add (b) {            alert (a+b);        }     function Sub (b) {            alert (a-b);        }    Add.call (Sub,3,1);    Add.apply (sub,[3,1]);

Their differences are written in the parameters of the call, respectively. The argument to apply is an array

The role and difference of 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.