What does the plus sign do in '+new Date'
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
JavaScript is loosely typed, so it performs type coercion/conversion in certain circumstances:
http://blog.jeremymartin.name/2008/03/understanding-loose-typing-in.html
http://www.jibbering.com/faq/faq_notes/type_convert.html
Other examples:
>>> +new Date()
1224589625406
>>> +"3"
3
>>> +true
1
>>> 3 == "3"
true
Here is the specification regarding the "unary add" operator. Hope it helps...
链接地址: http://www.djcxy.com/p/13578.html上一篇: 在Javascript中获取UTC时间戳
下一篇: '+新日期'中的加号是做什么的