2017-09-23 5 views
0

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.

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

私はあなたがなぜ「* ...仕様書に明白に述べているのか分からないのですか?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

+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

「+ date」が短くなりました。 – RobG

0

NaNは特別ですタイプ番号の値ではなく、「数値ではないものすべて」ではありません。これは、Math.sqrt(-1)を呼び出すなど、失敗したときに数値を返す関数で使用されます。

さらに、関数が数値を必要とする場合、JavaScriptは何を与えるにしても何度も行うのが最善です。 Dateは数値表現(Date.valueOfを使用して取得できます)を使用しているため、それを使用します。そのため型強制、数学の

(余談興味深いとして、あなたは値がisNaN機能を使用してNaNであるかどうかをテストすることができますが、直接NaNにそれを比較することfalseを返します。JavaScriptのものそれらのちょうど1)

関連する問題