All functions have the following methods: Apply () and call ()

Source: Internet
Author: User

The ecmascript Specification defines two methods for all functions: Apply () and call (). You can use these two methods to call a function just like calling methods of other objects.

Call ():

The first parameter of call () is the object of the function to be called. In the function body, this parameter is the value of the keyword "this. The remaining parameters of call () are the parameter values passed to the function to be called. For example, to pass two numbers to function f () and call it as the method of object o, you can use the following code:

F. Call (O, 1, 2 );

This is similar to the following code:

O. M = f; O. M (1, 2); Delete o. m;

Apply ():

The first parameter of apply () is also the object of the function to be called, except that the parameter to be passed to the function is specified by the array:

F. Apply (O, [1, 2]);

For example, to find the maximum number in a number array, you can use the apply () method to pass the array element to the math. Max () function:

<HTML>  

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.