Home > Web Front-end > JS Tutorial > body text

Introduction to the problems and differences between apply and Math.max() functions in js

亚连
Release: 2018-05-28 17:10:41
Original
1695 people have browsed it

This article mainly introduces the issues of apply and Math.max() functions in js. This article brings you two answers. Each answer is introduced to you in great detail. At the bottom of the article, js is mentioned. Friends who are interested should take a look at the difference between Math.max.apply and Math.max.

The following will introduce to you the problems of apply and Math.max() functions in js. The specific content is as follows:

var arr=[1,3,6,3,7,9,2];
console.log(Math.max.apply(null,arr));
Copy after login

I still don’t understand why the maximum value of an array can be calculated in this way? I still can't figure it out, please give some advice from a js expert.

Answer 1

Function.apply() is an OOP feature of JS. It is generally used to simulate inheritance and extending this. For The above code can be understood like this:

XXX.apply is a method of calling a function, and its parameters are: apply(Function, Args),

Function is the method to be called, Args is the parameter list. When Function is null, the default is as above.
is

Math.max.apply(null, arr)
Copy after login

which can be considered as

apply(Math.max, arr)
Copy after login

Then, arr is a parameter list. For the max method, its parameters are several numbers, that is,

Math.max(a, b, c, d, ...)
Copy after login
Copy after login

When using apply, add all parameters to an array, that is,

arr = [a, b, c, d, ...]
Copy after login

is substituted into the original formula,

Math.max.apply(null, [a, b, c, d, ...])
Copy after login

is actually equivalent to

Math.max(a, b, c, d, ...)
Copy after login
Copy after login

Here, the advantage of using apply is to improve performance in some JS engines.

Answer 2

Math.max() method supports passing multiple parameters, such as: Math.max(1,4,2,3,7,5,6)

But it does not support directly passing an array as a parameter, such as: Math.max(new Array( 1,4,2,3,7,5,6)).

Here, as long as we have a way to split the array one by one and pass it to the Math.max() method, the method of passing the array is implemented.

All functions have the apply (scope chain, parameter) method. The "parameter" of this function receives an array, and separates each value in the array and passes it to the caller

Supplement:

The difference between Math.max.apply and Math.max in Javascript

Javascript The Math.max method in can find the largest number among the given parameters.

> Math.max('1','2','3.1','3.2')
< 3.2
> Math.min(1,0,-1)
< -1
Copy after login


#But if it is an array, it cannot be called like this.

The apply method is used at this time:

apply 方法 (Function) (JavaScript)
调用函数,并用指定对象替换函数的 this 值,同时用指定数组替换函数的参数。
apply([thisObj[,argArray]])
thisObj
  可选。 要用作 this 对象的对象。
argArray
Copy after login

Optional. A set of arguments to be passed to the function.
Cleverly make the array also call Math.max and Math.min.

> Math.max.apply(null, [&#39;1&#39;,&#39;2&#39;,&#39;3.1&#39;,&#39;3.2&#39;])
< 3.2
> Math.min.apply(null, [1,0,-1])
< -1
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax upload method to implement js processing based on server-side return data

Double-layer ajax embedding Set (can be multi-layered) usage examples

Ajax implementation of pop-up non-refresh city selection function code

The above is the detailed content of Introduction to the problems and differences between apply and Math.max() functions in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!