'+new Date' 中的加号有什么作用 [英] What does the plus sign do in '+new Date'

查看:31
本文介绍了'+new Date' 中的加号有什么作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个地方看到了这一点

function fn() {返回+新日期;}

而且我可以看到它返回的是时间戳而不是日期对象,但我找不到任何关于加号正在做什么的文档.

谁能解释一下?

那是 + 一元运算符,它等价于:

function(){ return Number(new Date);}

见:http://xkr.us/articles/javascript/unary-add/>

在 MDN 中:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus

I've seen this in a few places

function fn() {
    return +new Date;
}

And I can see that it is returning a timestamp rather than a date object, but I can't find any documentation on what the plus sign is doing.

Can anyone explain?

解决方案

that's the + unary operator, it's equivalent to:

function(){ return Number(new Date); }

see: http://xkr.us/articles/javascript/unary-add/

and in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus

这篇关于'+new Date' 中的加号有什么作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆