javascript 请解释为什么以及如何 +new Date(); 在 IE8 或更低版本中用作 Date.now() 的“解决方法”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9430357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Please explain why and how +new Date(); works as 'workaround' for Date.now() in IE8 or below
提问by Jan Carlo Viray
(I'm reading the book "Professional JavaScript for Web Developers" to give a context about this question, specifically Chapter 5 on Reference Types)
(我正在阅读“ Professional JavaScript for Web Developers”一书以提供有关此问题的上下文,特别是关于引用类型的第 5 章)
I'm wondering why and how
var start = +new Date();
works to get the current millisecond representation of now as a work-around to browsers (e.g.: IE8) that don't support ECMAScript 5's Date.now()
?
我想知道为什么以及如何
var start = +new Date();
将 now 的当前毫秒表示作为不支持 ECMAScript 5 的浏览器(例如:IE8)的变通方法Date.now()
?
What does the +
operator do here compared to just plain old new Date()
which also gets the current date and time?
+
与new Date()
同样获取当前日期和时间的普通老式相比,操作员在这里做什么?
回答by anddoutoi
What happens is that you first create a new Date object and then cast it to a number.
发生的情况是您首先创建一个新的 Date 对象,然后将其转换为一个数字。
TL;DR-version
TL;DR-版本
Under the hood the runtime calls valueOf
method of the Date
object.
Verbose-version
详细版本
return a new Date
object
返回一个新Date
对象
var d = new Date;
use the Unary + Operator
使用一元 + 运算符
var n = +d;
The unary + operator calls the internal ToNumberwith d
.
一元+运算符调用内部ToNumber用d
。
Takes an inputargument and if the argument typeis Object
(Date is) call the internal ToPrimitivewith inputand hint Number.
接受一个输入参数,如果参数类型是Object
(Date is) 调用内部ToPrimitive与输入和提示 Number。
takes an inputargument and an optional argument PreferredType.
接受一个输入参数和一个可选参数PreferredType。
if input typeis Object the spec says:
如果输入类型是 Object 规范说:
Return a default value for the Object. The default value of an object is retrieved by calling the [[DefaultValue]]internal method of the object, passing the optional hint PreferredType. The behaviour of the [[DefaultValue]]internal method is defined by this specification for all native ECMAScript objects in 8.12.8.
返回对象的默认值。通过调用对象的[[DefaultValue]]内部方法,传递可选的提示PreferredType来检索对象的默认值。[[DefaultValue]]内部方法的行为 由本规范为 8.12.8 中的所有原生 ECMAScript 对象定义。
8.12.8 [[DefaultValue]] (hint)
When the [[DefaultValue]] internal method of O is called with hint Number, the following steps are taken:
- Let valueOf be the result of calling the [[Get]] internal method of object O with argument "valueOf".
- If IsCallable(valueOf) is true then,
- Let val be the result of calling the [[Call]] internal method of valueOf, with O as the this value and an empty argument list.
- If val is a primitive value, return val.
当使用提示Number调用O的[[DefaultValue]]内部方法时,采取以下步骤:
- 令 valueOf 是使用参数“valueOf”调用对象 O 的 [[Get]] 内部方法的结果。
- 如果 IsCallable(valueOf) 为真,则
- 令 val 为调用 valueOf 的 [[Call]] 内部方法的结果,其中 O 作为 this 值和一个空参数列表。
- 如果 val 是原始值,则返回 val。
In code this approximately translates to:
在代码中,这大约转换为:
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]]
ting the internal method of O with argument "valueOf" basically means returning Date.prototype.valueOf.
[[Get]]
使用参数“valueOf”调用 O 的内部方法基本上意味着返回Date.prototype.valueOf。
15.9.5.8 Date.prototype.valueOf ( )
15.9.5.8 日期.prototype.valueOf ( )
The
valueOf
function returns a Number, which is this time value.
该
valueOf
函数返回一个 Number,即这个时间值。
If we now go back to 9.3 ToNumberwe see that ToNumber calls itself, this time with the returned val
from 8.12.8 [[DefaultValue]] (hint)as primValue
. If argument typeis Number it says:
如果我们现在回到9.3 ToNumber,我们会看到 ToNumber 调用自己,这次是val
从8.12.8 [[DefaultValue]] (hint)as 返回的primValue
。如果参数类型是 Number 它说:
The result equals the input argument (no conversion).
结果等于输入参数(无转换)。
The End
结束
回答by mr.boyfox
Date.now() function on IE:
IE 上的 Date.now() 函数:
return a number of milliseconds between midnight, January 1, 1970, and the current date and time.
Requirements
要求
Not supported in installed versions earlier than Internet Explorer 9. However, it is supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards. Also supported in Windows Store apps.
For get current Date object on IE8, you can use this:
要在 IE8 上获取当前日期对象,您可以使用:
if (typeof Date.now() === 'undefined') {
Date.now = function () {
return new Date();
}
}
For get time value in a Date Object(as the number of milliseconds since midnight January 1, 1970.) on IE8, you can use this:
要在 IE8 上获取日期对象中的时间值(作为自 1970 年 1 月 1 日午夜以来的毫秒数),您可以使用以下命令:
var currentDateTime = +new Date();