The relationship between call and apply and shallow solutions

Source: Internet
Author: User

In layman's words, call and apply allow a function to invoke the properties and methods of other functions, just passing in the parameters differently.

1. Use apply to the method of the other function, the passed parameter is a collection of all parameters, such as the following code

function aaa (name,sex,height) {      bbb.apply (This, arguments);  at This point the AAA,BBB is the same, the BBB will output ' 170cm '}; // call function, pass back the parameter aaa (' Zhang San ', ' Male ', ' 170cm ')             ; function BBB (name,sex,height) {    alert (height);};   

To output the parameter is height, you need to change the BBB into the parameter

2. Use the call method to call the method of other functions, the parameters passed in is a parameter, such as the following code

function aaa (name, sex) {    Bbb.call (this// ) The parameters returned by the call method are objects, directly selecting the parameters that need to be returned;}; // call function, pass back the parameter aaa (' Zhang San ', ' Male ', ' 170cm '); function bbb (height) {    alert (height);};

Summary: The difference between the two is that the form of the Apply return parameter is a parameter group, and the call method returns the parameter as a single Parameter object.

The relationship between call and apply and shallow solutions

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.