意外的 Javascript 日期对象隐式转换

Unexpected Javascript Date object implicit conversion

为什么 Javascript 的日期对象 returns 隐式转换的不同值?

数字转换:

+new Date()
// returns 1456293356618 as expected

字符串转换:

''+new Date()
// returns "Wed Feb 24 2016 09:26:28 GMT+0" but "1456293356618" as a string was expected 

在哪里可以找到关于 ECMAScript 的文档和 v8 源代码的实现?

编辑:我不是在寻找预期结果的解决方案。我想在规范中找到文档。

我想你指的是这个Overview of Date Objects and Definitions of Abstract Operators,特别是第 20.3.1.1 节

A Date object contains a Number indicating a particular instant in time to within a millisecond. Such a Number is called a time value. A time value may also be NaN, indicating that the Date object does not represent a specific instant of time.

这意味着对 Date 对象使用数学运算将提取它的 Number 值来工作。这就是为什么像 +new Date()Math.floor(new Date()) returns a Number.

这样的语句

至于 '' + new Date(),日期对象 returns 其字符串值可能使用其 toString() 函数。

+ 运算符重载。在:

+new Date()

它被视为 unary + operator 并将值强制转换为 Number。在:

'' + new Date() // note one value is a string

它被视为 string concatenation operator 并将值强制转换为字符串。在:

5 + 6   // note both values are number

它被视为addition operator。由于值是数字,因此不需要强制转换。

请注意,+ 是否进行加法或连接取决于值,并在 ECMAScript 2015 §12.7.3.1 step 11 中进行了描述。