2017-09-23 41 views
0

我不是100%確定爲什麼您能夠使用Math.max()函數計算多個Date對象的最大日期。我的IDE PhpStorm不斷給我下面的錯誤:使用Math.max()計算最大JavaScript日期對象文檔

Argument type Date is not assignable to parameter type number

在我的代碼片段說明它是分配給參數:

/* Variable Defaults */ 
 
var dateOne = new Date(); 
 
var dateTwo = new Date('2017-01-21'); 
 
var dateThree = new Date('11/16/2016'); 
 
var dateMax = Math.max(dateOne, dateTwo, dateThree); 
 

 
/* Console Data */ 
 
console.log('dateOne', dateOne); 
 
console.log('dateTwo', dateTwo); 
 
console.log('dateThree', dateThree); 
 
console.log('dateMax', dateMax + ': ' + new Date(dateMax));

我決定尋找到了規範來查看我的IDE是否使用了較老的標準,但是我的研究未能實現我對自己爲什麼要使用這種方法的願望:

ECMAScript 1st Edition (ECMA-262)

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.

ECMAScript 5.1 (ECMA-262)

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.

ECMAScript 2015 (6th Edition, ECMA-262)

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.

ECMAScript Latest Draft (ECMA-262)

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 except that +0 is considered to be larger than -0.

我已經在所有現代瀏覽器中測試過這種方法,並沒有產生任何錯誤。雖然我不知道這是否與舊版瀏覽器兼容。

爲什麼Math.max()的工作是通過傳遞Date對象時的規範明確指出它不應該這樣做?

+1

因爲強制類型轉換,數學.max,需要數字。所以它會將日期轉換爲數字。所以PHPStorm是錯誤的,它看起來像Intellisense,正在說胡話。 – Keith

+0

我不知道你爲什麼認爲「* ...規範明確表明它不應該?」當他們沒有人說。除了第一個,說「* ...對每個參數調用ToNumber ... *」(這是沒有這麼說的編輯1)。要了解[* ToNumber *](http://ecma-international.org/ecma-262/8.0/#sec-tonumber)如何處理日期,請點擊鏈接。簡而言之,它會調用* valueOf *,它會返回內部的[*時間值*](http://ecma-international.org/ecma-262/8.0/#sec-time-values-and-time-range ),這是一個數字(和*可能是'NaN',BTW)。 – RobG

+0

@RobG謝謝你的解釋。我現在看到了。當我問這個問題時,我的印象是'Date'對象是'NaN'。顯然,我錯了。 – Daerik

回答

2

由於有效Date()不是NaN,它可以被轉換爲使用​​功能,+一元運算符或valueOf()功能的數目:

var date = new Date(); 
 
console.log(isNaN(date) + ',' + Number(date)); 
 
console.log(isNaN(date) + ',' + +date); 
 
console.log(isNaN(date) + ',' + date.valueOf()); 
 
console.log(isNaN(2) + ',' + Number(2)); 
 
console.log(isNaN('2') + ',' + Number('2')); 
 
console.log(isNaN('xx') + ',' + Number('xx')); 
 
console.log(isNaN(['a']) + ',' + Number(['a'])); 
 
console.log(isNaN({}) + ',' + Number({}));

+1

你也可以使用'date.valueOf()'來做到這一點。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf –

+0

@DuncanThacker你說得對。 –

+1

較短是'+日期'。 – RobG

0

NaN是一個特殊的類型號的值,而不是「不是數字的所有東西」。它被功能所使用,當它不能這樣做時應該返回一個數字,例如,如果您致電Math.sqrt(-1)。此外,如果一個函數需要一個數字,那麼JavaScript會盡最大努力製作一些你給它的東西。並且由於Date具有數字表示(可以使用Date.valueOf獲得),它將使用該表示。

(作爲一個有趣的不談,你可以測試一個值是否NaN使用isNaN功能,但相比它NaN將直接返回false。那些JavaScript的事情只是一個)