Ich bin nicht 100% sicher, warum Sie das maximale Datum aus mehreren Date Objekten mithilfe der Math.max()-Funktion berechnen können. Meine IDE PhpStorm
hält mir den folgenden Fehler geben:Verwenden von Math.max() zum Berechnen der maximalen JavaScript-Date-Objekt-Dokumentation
Argument type Date is not assignable to parameter type number
Es ist zuordenbare Parameter wie in meinem Snippet veranschaulicht:
/* 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));
Ich habe beschlossen, in die schauen Spezifikationen, um zu sehen, ob vielleicht meine IDE einen älteren Standard verwendete, aber meine Forschung hatte meinen Wunsch nicht erfüllt, mich zu erziehen, warum diese Methode in erster Linie funktionieren sollte:
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.
Ich habe diese Methode in allen modernen Browsern getestet und es hat keine Fehler erzeugt. Allerdings frage ich mich, ob das mit älteren Browsern kompatibel ist.
Warum funktioniert Math.max()
durch Übergeben von Date
Objekte, wenn die Spezifikationen klar sagen, dass es nicht sollte?
Wegen Art Zwang, Math .max, nimmt Zahlen. Es wird also nur die Daten in Zahlen umwandeln. Also PHPSstorm ist falsch, es sieht so aus als wäre es Intellisense, redet Unsinn. – Keith
Ich weiß nicht, warum Sie denken "* ... die Spezifikationen sagen eindeutig, dass es nicht sollte? *", Wenn keiner von ihnen das sagt.Bis auf die erste, das sagen "* ... ruft ToNumber auf jedes der Argumente ... *" (was ed 1 tat, ohne es zu sagen). Um zu sehen, wie [* ToNumber *] (http://ecma-international.org/ecma-262/8.0/#sec-tonumber) Daten behandelt, folgen Sie den Links. Die kurze Antwort lautet: * valueOf *, was den internen [* time value *] (http://ecma-international.org/ecma-262/8.0/#sectime-values-and-time-range) zurückgibt), das ist eine Zahl (und * könnte * sein 'NaN', BTW). – RobG
@RobG Vielen Dank für Ihre Erklärung. Das sehe ich jetzt. Zu der Zeit, als ich diese Frage stellte, hatte ich den Eindruck, dass ein "Date" -Objekt "NaN" war. Klar, ich habe mich geirrt. – Daerik