The front end Review--math.min.apply (Math,arr) Writing comprehension

Source: Internet
Author: User

Math.min.apply (Math,arr) This method is used to find the smallest value in an array, which puzzles me, and I don't know why the first parameter applied to the Math object is passed back in.

"JavaScript advanced Programming" said: in order to correctly set the value of this.

This is not a good explanation, at least for the present stage of me.

(1) to the object-oriented, I first studied the Math object, from the words "javascript",

The Math is a little different form the other built-in objects, because it cannot be used as a constructor to create objects.< c1> It is just a collection of functions and constants.

This seems to tell us that Math is an object, that Min and Max are its methods, and that both Min and Max accept parametric forms, Math.min (NUM1, num2, num3, ...) That's all.

(2) but Math.min.apply (Math,arr) This strange wording is what. Well, we need to look at the second piece, the use of apply.

What is the role of call and apply? In addition to changing the this point of the function, what else. ---Apply can change the form passed to the function parameter

As you can see from the MDN, this is a magical part of apply.


Another key point to understand:

The first parameter of apply, can not be ignored ...

If the IS-a function in non-strict mode code, null and undefined would be replaced with the global object, and Primi tive values would be boxed

Summary: For the normal function FN,

function fn (Arg1,arg2,arg3,...) {
//....
}

FN (1,2,3)

Fn.apply. (null,[1,2,3])

Apply changes the incoming form of the parameter
















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.