ecmascript--reference data type function about call and apply

Source: Internet
Author: User

call, apply:
role:
1, change the This in the function, and take the value of the first parameter as this
of the current function 2, let the current function execute
For example: Fn.call (obj) makes the this in FN change to obj, and then makes the FN function execute

difference: When you pass parameters to a function, call is a pass, and apply is to put the parameters that need to be passed into an array, and pass in the

function name. apply ([This],[parameter1,parameter2 ...])

call/apply: The first argument writes who, this is who in the function, but there is a special case:



The first argument is who it is, Write null is NULL, write undefined is undefined, do not write is undefined

Applications for Call and apply:

Gets the maximum and minimum values in the array

var ary = [12, 25, 13, 26, 27, 59, 56];

The use of the Max and Min methods in mathematical functions is implemented by using the Apply method when executing, you need to pass the parameters as an array to pass in

var ary = [N, a, 56];var max = Math.max.apply (null, ary), var min = Math.min.apply (null, ary); console.log (min, max);    59

  





ecmascript--reference data type function about 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.