Math.max can be implemented to get the largest item in the array
var array = [1,2,3,4,5]; var max = Math.max.apply (null// 5
Called when the first parameter gave a null, this is because there is no object to call this method, only need to use this method to help the operation, to get the result returned on the line, so directly passed a null past.
Math.min can be implemented to get the smallest item in the array
var array = [1,2,3,4,5]; var min= Math.min.apply (null, array); Console.log (min); 1
Add the Max and Min method above the native object
Then you'll need to use the native object method the Object.defineproperty () method defines a new property directly on an object, or modifies an existing property of an object and returns the object.
Add a method above the native object, and the for loop will not find the
Object.defineproperty (Array.prototype, ' Max '), {writable:false, Enumerable:false, Configurable:true, Value:function () { returnMath.max.apply (NULL, This); } }); Object.defineproperty (Array.prototype,' Min ', {writable:false, Enumerable:false, Configurable:true, Value:function () { returnMath.min.apply (NULL, This); } });
How to use it, call it directly on the array
var arr = [54,545,2165,545,56]; Console.log (Arr.max ());
Clever usage of Javascript apply