JavaScript 存储为字符串后日期不匹配

JavaScript Date mismatch after being stored as string

我无法弄清楚为什么 ls_a === a 在下面的代码中返回 false。似乎当我将日期转换为字符串并返回日期时,有些东西丢失了,但是什么??

JSFiddle:http://jsfiddle.net/s6accbax/

var a = new Date();
localStorage.a = a.getTime();
ls_a = new Date(parseInt(localStorage.a));

console.log(a);    // Fri Jun 12 2015 22:12:34 GMT-0600 (MDT)
console.log(ls_a); // Fri Jun 12 2015 22:12:34 GMT-0600 (MDT)
console.log(ls_a === a); // returns false!?!?!
console.log(ls_a.getTime() === a.getTime()); // returns true as expected

副本:JavaScript Date Object Comparison

这是因为 ls_aa 是不同的对象,当您调用 .getTime() 时,您得到的字符串未作为对象进行比较

使用===时没有类型转换,因此ls_a不等同于a.

The strict equality operator === only considers values equal that have the same type.