Number ObjectNameNumber Object---place-holder for numeric constantsSynopsis
Number.constant Constructor
new Number(value) Arguments
ReturnsThe newly constructed Number object Constants
Methods
DescriptionNumbers are a basic, primitive data type in JavaScript. In Navigator 3.0, however, JavaScript also supports the Number object, an object type that represents a primitive numeric value. JavaScript automatically converts between the primitive and object forms as necessary. In Navigator 3.0, you can explicitly create a Number object with the Number() constructor, although there is rarely any need to do so. The Number() constructor is actually more commonly used as a place-holder for five useful numeric constants: the largest and smallest representable numbers, positive and negative infinity, and the special Not-a-Number value. Note that these values are properties of the Number() constructor function itself, not of individual number objects. For example, you use the MAX_VALUE property as follows:
biggest = Number.MAX_VALUE
n = new Number(2); biggest = n.MAX_VALUE By contrast, the toString() method of the Number object is a method of each Number object, not of the Number() constructor function. As noted above, JavaScript automatically converts from primitive numeric values to Number objects whenever necessary. This means that we can use the toString() method with a variable that holds a number, even though that value is not actually an object:
value = 1234; binary_value = n.toString(2); What happens in this code is that JavaScript implicitly invokes the Number() constructor to convert the number n to a temporary Number object for which the toString() method can be invoked. It is this toString() method that is the main reason for the existence of the Number object in the first place. |
|