The call () method of the function and the Apply () method

Source: Internet
Author: User

Read the source code of the various frameworks the apply and call methods must often see that a good understanding of the concept is crucial.

Functions in JavaScript are also objects, which are no different from other JavaScript objects, and function objects can also contain methods. The call () method and the Apply () method can indirectly invoke the function. Two methods allow you to explicitly specify the this value that is required for the invocation, that is, any function can be invoked as a method of any object, even if the function is not a method of that object.

Two methods can specify the arguments that are called. The call () method uses its own argument list as the argument for the function, and the Apply () method requires that the parameter be passed in as an array.

The first argument of the call () method and the Apply () method is the parent object that is to invoke the function, which is the emphasis context through which this is used in the body of the function to obtain a reference to it.

function A1 (i,j) {    var a = 1;    Console.log (a);    Console.log (I,J);} function B1 (i,j) {    var b = 2;    Console.log (i,j);      This (I,J);} B1.call (A1,111,222);   Call () method invocation //  b1.apply (a1,[111,222]), apply () method call, same as, only the form of the argument is not the same

The call () method of the function and the Apply () method

Related Article

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.