Control function calls in JS: Call (), apply () and bind ()

Source: Internet
Author: User

All functions have call (), apply (), and bind () methods. They can point to this with a value when executing the method and change the object-oriented scope.

Apply method:

The following two types of expressions are equivalent:

Func (ARG1,ARG2,ARG3)

Func.apply (Null,[arg1,arg2,arg3])

Apply () is often used when a function needs to accept multiple arguments in the form of an array.

Use Math.max () to get the largest element in an array:

Math.max.apply (null,[17,33,21]);

Call Method:

Expression: Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])

Definition: Invokes one method of an object, replacing the current object with another object.

Description: Call () can achieve a variety of functions, such as 1. The ShowName () method that originally belonged to the animal object is given to another object, cat, to use; 2. Implementing Inheritance (Single-inheritance, multiple-inheritance) 3. Replace one function with another.

Bind method:

Http://www.zhangxinxu.com/wordpress/2012/10/ecmascript-es5-bind-array-slice-call-apply/is the most comprehensive, The most interesting explanation about the bind method is that it is not better to teach than to grant the fish.

    

Control function calls in JS: Call (), apply () and bind ()

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.