javascript - Equality comparison between Date and number doesn't work -
according ecma script standard, following code should return true, doesn't:
d = new date() ; d.settime(1436497200000) ; alert( d == 1436497200000 ) ; section 11.9.3 says:
- if type(x) either string or number , type(y) object, return result of comparison x == toprimitive(y).
then, section 8.12.8 says toprimitive retuns result of valueof method. means last line in example above should equivalent to:
alert( d.valueof() == 1436497200000 ); which return true, indeed.
why first case not return true?
if @ spec @ section 8.12.8, find text near end of section:
when
[[defaultvalue]]internal method ofocalled no hint, behaves if hintnumber, unlessodateobject (see 15.9.6), in case behaves if hintstring.
(emphasis mine)
now, in step 8 / 9 of the abstract equality comparison algorithm [11.9.3], toprimitive(x) , toprimitive(y) called without hint parameter.
the lack of hint parameter, above text, means toprimitive method returns tostring() value, on date objects.
as you're aware, (new date()).tostring() returns string representation of date in american english [source]:
"wed jul 01 2015 22:08:41 gmt+0200 (w. europe daylight time)"
that string doesn't equal 1436497200000 shouldn't come big surprise. ;-)
Comments
Post a Comment