JS documentation of numerical syntax
I have seen a syntax such as the following before:
var mynum = new Number();
var temp = (+mynum); //this line is what i am curious about
var text = temp.toPrecision(3);
Can anyone tell me what this + syntax means? What I have f开发者_如何学Goound is that in some JS implementations, it is somehow necessary as it ensures that the number defined in mynum is valid.
Thanks, jml
+
is a unary operator which is used to coerce data types into numbers. Unary meaning it only needs one operand.
new Date
returns an object, applying +
coerces it into a timestamp eg 1277504628812
new Number
returns an object, applying +
coerces it into the numeric literal 0
.
See: http://bclary.com/2004/11/07/#a-11.4.6
This is the ECMAScript documentation, which is the subset of Javascript, in HTML format.
精彩评论