以前没接触到这个用法,也就是前边的+ 特此记录一下
JavaScript中可以在某个元素前使用 ‘+’ 号,这个操作是将该元素转换秤Number类型,如果转换失败,那么将得到 NaN。
所以 +new Date 将会调用 Date.prototype 上的 valueOf 方法,而根据 MDN ,Date.prototype.value 方法等同于 Date.prototype.getTime() 。
所以下列代码效果相同:
console.log(+new Date);
console.log(new Date().getTime());
console.log(new Date().valueOf());
console.log(new Date() * 1);
本文揭示了JavaScript中在元素前加'+'用于Date对象的时间戳转换技巧,通过实例展示了+newDate与getTime(), valueOf()和*1等方法的等效性。

2067

被折叠的 条评论
为什么被折叠?



