发生的情况是,您首先创建一个新的 Date 对象,然后将其转换为数字。
TL;DR 版本
运行时调用的幕后valueOf https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/valueOf的方法Date https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date object.
详细版本
返回一个新的Date https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date object
var d = new Date;
use the 一元+运算符 http://es5.github.com/#x11.4.6
var n = +d;
一元 + 运算符调用内部ToNumber http://es5.github.com/#x9.3 with d
.
9.3 转号 http://es5.github.com/#x9.3
需要一个input论证并且如果参数类型 is Object
(日期是)致电内部到原语 http://es5.github.com/#x9.1 with input and 提示号.
9.1 到原语 http://es5.github.com/#x9.1
需要一个input参数和可选参数首选类型.
if 输入类型is Object 规范说:
返回对象的默认值。对象的默认值
通过调用来检索[[默认值]] http://es5.github.io/#x8.12.8的内部方法
对象,传递可选提示首选类型。该人的行为[[默认值]] http://es5.github.io/#x8.12.8内部方法由本规范定义
8.12.8 中的所有本机 ECMAScript 对象。
8.12.8 [[默认值]](提示) http://es5.github.io/#x8.12.8
当使用提示调用 O 的 [[DefaultValue]] 内部方法时
号,采取以下步骤:
- 令 valueOf 为使用参数“valueOf”调用对象 O 的 [[Get]] 内部方法的结果。
- If IsCallable(valueOf) is true then,
- 令 val 为调用 valueOf 的 [[Call]] 内部方法的结果,其中 O 作为 this 值和一个空参数列表。
- 如果 val 是原始值,则返回 val。
在代码中,这大约翻译为:
var val,
type,
valueOf = O.Get( 'valueOf' );
if ( typeof valueOf === 'function' ) {
val = valueOf.call( O );
type = typeof val;
if ( val == null || type === 'boolean' || type === 'number' || type === 'string' ) {
return val;
}
}
[[Get]]
使用参数“valueOf”来调用 O 的内部方法基本上意味着返回日期.prototype.valueOf http://es5.github.io/#x15.9.5.8.
15.9.5.8 Date.prototype.valueOf ( ) http://es5.github.io/#x15.9.5.8
The valueOf
函数返回一个数字,即这个时间值 http://es5.github.io/#this-time-value.
如果我们现在回到9.3 转号 http://es5.github.com/#x9.3我们看到 ToNumber 调用自身,这次返回的是val
from 8.12.8 [[默认值]](提示) http://es5.github.io/#x8.12.8 as primValue
. If 参数类型是数字,它说:
结果等于输入参数(无转换)。
The End