Math.max()関数を使用して複数のDateオブジェクトから最大日付を計算できる理由を100%確信していません。私のIDE PhpStorm
は私に次のエラーを与え続け:Math.max()を使用して最大JavaScript Dateオブジェクトドキュメントを計算する
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.
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
オブジェクトを渡すことによって機能するのですか?
.maxは数字をとります。だから、日付を数字に変換するだけです。だからPHPStormは間違っている、Intellisenseのように見えて、ナンセンスを話している。 – Keith
私はあなたがなぜ「* ...仕様書に明白に述べているのか分からないのですか?1番を除いて、 "* ...各引数のToNumberを呼び出す..."(これはed 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
@RobGあなたの説明をありがとう。私は今それを見る。私がこの質問をしたとき、私は「Date」オブジェクトがNaNであったという印象を受けました。明らかに、私は間違っていた。 – Daerik