2015-11-22 14 views
5

Tôi thực sự tò mò về cách các chức năng này thực sự hoạt động? Tôi biết có rất nhiều câu hỏi về cách sử dụng chúng, tôi đã biết cách sử dụng chúng, nhưng tôi không thể tìm thấy bất cứ nơi nào để thực sự thực hiện chức năng này trên một mảng, ví dụ, nếu không có chức năng như vậy ? Bạn sẽ viết mã như thế nào nếu không có người trợ giúp?Javascript Math.max và Math.min hoạt động như thế nào?

+0

Khởi tạo một giá trị; lặp lại trên bộ sưu tập, kiểm tra nếu mỗi phần tử lớn hơn hoặc nhỏ hơn giá trị cao nhất/thấp nhất hiện tại; nếu có, thay thế giá trị bằng phần tử đó. – grayshirt

Trả lời

1

chức năng cơ bản:

Math.max() và Math.min() được sử dụng trên số (hoặc những gì họ có thể ép buộc vào con số), bạn không thể trực tiếp vượt qua một mảng như một tham số.

Ex:

Math.max(1,52,28) 

Bạn có thể có một số lượng các số dấu phẩy phân cách.

Mảng:

Ví dụ này cho thấy cách người ta có thể áp dụng chúng vào mảng:

JavaScript: min & max Array values?

Về cơ bản các công việc sau:

Math.max.apply(null, [1,5,2,3]); 

Tại sao mà làm việc ?

Điều này hoạt động vì áp dụng là một hàm mà tất cả các hàm đều áp dụng một hàm có đối số của một mảng.

Math.max.apply (null, [1,5,2,3]) cũng giống như Math.max (1,5,2,3)

+1

Đừng quên, nó cũng hoạt động trên các đối tượng có cùng thuộc tính như một mảng: 'Math.max.apply (null, {0: 1,1: 4,2: 2, length: 3})' – ahitt6345

+1

@ ahitt6345 Đó là một điểm tốt; và điều đó có thể hữu ích. tất nhiên bạn phải làm Math.functionname.apply (null, obj); Tôi chỉ muốn chỉ ra rằng trong trường hợp ai đó tự hỏi tại sao chính xác sao chép mà không hoạt động. – csga5000

+0

Rất tiếc, đã quên mất chức năng tôi muốn áp dụng ... cảm ơn vì đã nói với tôi. : | lol – ahitt6345

2

Đây là mã Math.max trong Động cơ V8 của Chrome.

function MathMax(arg1, arg2) { // length == 2 
    var length = %_ArgumentsLength(); 
    if (length == 2) { 
    arg1 = TO_NUMBER(arg1); 
    arg2 = TO_NUMBER(arg2); 
    if (arg2 > arg1) return arg2; 
    if (arg1 > arg2) return arg1; 
    if (arg1 == arg2) { 
     // Make sure -0 is considered less than +0. 
     return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg2 : arg1; 
    } 
    // All comparisons failed, one of the arguments must be NaN. 
    return NaN; 
    } 
    var r = -INFINITY; 
    for (var i = 0; i < length; i++) { 
    var n = %_Arguments(i); 
    n = TO_NUMBER(n); 
    // Make sure +0 is considered greater than -0. 
    if (NUMBER_IS_NAN(n) || n > r || (r === 0 && n === 0 && %_IsMinusZero(r))) { 
     r = n; 
    } 
    } 
    return r; 
} 

Here là kho lưu trữ.

+0

Thực ra, [nó ở dạng JavaScript] (https://github.com/v8/v8/blob/cd81dd6d740ff82a1abbc68615e8769bd467f91e/src/js/math.js#L77-L102). –

+0

Cảm ơn. Tôi sẽ chỉnh sửa câu trả lời. –

0

này rất dễ thực hiện với Array.prototype.reduce:

function min() { 
    var args = Array.prototype.slice.call(arguments); 

    var minValue = args.reduce(function(currentMin, nextNum) { 
     if (nextNum < currentMin) { 
     // nextNum is less than currentMin, so we return num 
     // which will replace currentMin with nextNum 
     return nextNum; 
     } 
     else { 
     return currentMin; 
     } 
    }, Infinity); 

    return minValue; 
} 
+0

Bạn không cần phải cắt đầu tiên; bạn chỉ có thể làm 'Array.prototype.reduce.call'. –

2

Dưới đây là làm thế nào để thực hiện các chức năng nếu Math.min()Math.max() đã không tồn tại.

Các hàm có đối tượng arguments, mà bạn có thể lặp lại để lấy giá trị của nó.

Điều quan trọng cần lưu ý là Math.min() không có đối số trả Infinity, và Math.max() không có đối số trả -Infinity.

function min() { 
 
    var result= Infinity; 
 
    for(var i in arguments) { 
 
    if(arguments[i] < result) { 
 
     result = arguments[i]; 
 
    } 
 
    } 
 
    return result; 
 
} 
 

 
function max() { 
 
    var result= -Infinity; 
 
    for(var i in arguments) { 
 
    if(arguments[i] > result) { 
 
     result = arguments[i]; 
 
    } 
 
    } 
 
    return result; 
 
} 
 

 
//Tests 
 
console.log(min(5,3,-2,4,14));  //-2 
 
console.log(Math.min(5,3,-2,4,14)); //-2 
 

 
console.log(max(5,3,-2,4,14));  //14 
 
console.log(Math.max(5,3,-2,4,14)); //14 
 

 
console.log(min());     //Infinity 
 
console.log(Math.min());    //Infinity 
 

 
console.log(max());     //-Infinity 
 
console.log(Math.max());    //-Infinity

1

Vâng, đây là min mà không Math.min (code đang trong ES6).

function min() { 
    return Array.from(arguments).reduce(
    (minSoFar, next) => minSoFar < next ? minSoFar : next 
    , Infinity) 
} 

Cùng một logic có thể được triển khai với một vòng lặp đơn giản. Bạn sẽ chỉ cần theo dõi một biến thông qua phép lặp của bạn, đó là giá trị thấp nhất mà bạn đã thấy cho đến nay. Giá trị ban đầu của minSoFar sẽ là Infinity. Lý do là bất kỳ Số trừ Infinity là ít hơn Infinity, nhưng trong trường hợp của không đối số gửi đi, chúng tôi muốn trở lại Infinity bản thân, bởi vì đó là những gì Math.min() không có đối số để đánh giá.

function min() { 
    let minSoFar = Infinity 
    for(let i = 0, l = arguments.length; i < l; i++) { 
    const next = arguments[i] 
    minSoFar = minSoFar < next ? minSoFar : next 
    } 
    return minSoFar 
} 

Max có thể được thực hiện với khá nhiều cùng một logic, chỉ có bạn đang theo dõi các giá trị cao nhất mà bạn đã nhìn thấy cho đến nay, và các giá trị ban đầu là -Infinity. (! Mà có thể/sẽ giúp bạn trong việc thực hiện)

3

Chúng ta hãy nhìn vào thông số kỹ thuật

Trong ECMAScript 1st Edition (ECMA-262) (các định nghĩa ban đầu cho cả Math.max/phút), chúng ta thấy như sau:

15.8.2.11 max(x, y) 
Returns the larger of the two arguments. 
    • If either argument is NaN, the result is NaN. 
    • If x>y, the result is x. 
    • If y>x, the result is y. 
    • If x is +0 and y is +0, the result is +0. 
    • If x is +0 and y is −0, the result is +0. 
    • If x is −0 and y is +0, the result is +0. 
    • If x is −0 and y is −0, the result is −0. 

15.8.2.12 min(x, y) 
    Returns the smaller of the two arguments. 
    • If either argument is NaN, the result is NaN. 
    • If x<y, the result is x. 
    • If y<x, the result is y. 
    • If x is +0 and y is +0, the result is +0. 
    • If x is +0 and y is −0, the result is −0. 
    • If x is −0 and y is +0, the result is −0. 
    • If x is −0 and y is −0, the result is −0. 

các phiên bản sau của đặc tả cho chúng ta:

ECMAScript 5.1

15.8.2.11 max ([ value1 [ , value2 [ , … ] ] ]) 

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values. 

    • If no arguments are given, the result is −∞. 
    • If any value is NaN, the result is NaN. 
    • The comparison of values to determine the largest value is done as in 11.8.5 except that +0 is considered to be larger than −0. 

    The length property of the max method is 2. 

15.8.2.12 min ([ value1 [ , value2 [ , … ] ] ]) 

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values. 

    • If no arguments are given, the result is +∞. 
    • If any value is NaN, the result is NaN. 
    • The comparison of values to determine the smallest value is done as in 11.8.5 except that +0 is considered to be larger than −0. 

    The length property of the min method is 2. 

Các tham chiếu đến 11.8.5 có thể được tìm thấy ở đây: The Abstract Relational Comparison Algorithm

ECMAScript 2015

20.2.2.24 Math.max (value1, value2 , …values) 

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values. 

    • If no arguments are given, the result is −∞. 
    • If any value is NaN, the result is NaN. 
    • The comparison of values to determine the largest value is done using the Abstract Relational Comparison algorithm (7.2.11) except that +0 is considered to be larger than −0. 

    The length property of the max method is 2. 

20.2.2.25 Math.min (value1, value2 , …values) 

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values. 

    • If no arguments are given, the result is +∞. 
    • If any value is NaN, the result is NaN. 
    • The comparison of values to determine the smallest value is done using the Abstract Relational Comparison algorithm (7.2.11) except that +0 is considered to be larger than −0. 

The length property of the min method is 2. 

Và một lần nữa, 7.2.11 có thể được tìm thấy ở đây: Abstract Relational Comparison

Các vấn đề liên quan