Core JavaScript 1.5 Reference:Global Objects:Number
From MDC
目录 |
[编辑] 摘要
核心对象
Lets you work with numeric values. The Number object is an object wrapper for primitive numeric values.
[编辑] Created by
Number对象构造器
new Number(value)
[编辑] Parameters
-
value - The numeric value of the object being created.
[编辑] Description
The primary uses for the Number object are:
- To access its constant properties, which represent the largest and smallest representable numbers, positive and negative infinity, and the Not-a-Number value.
- To create numeric objects that you can add properties to. Most likely, you will rarely need to create a
Numberobject.
The properties of Number are properties of the class itself, not of individual Number objects.
JavaScript 1.2: Number(x) now produces NaN rather than an error if x is a string that does not contain a well-formed numeric literal. For example, the following prints NaN:
x=Number("three");
document.write(x + "<BR>");
You can convert any object to a number using the top-level Number function.
[编辑] Properties
constructor: Specifies the function that creates an object's prototype.
MAX_VALUE: The largest representable number.
MIN_VALUE: The smallest representable number.
NaN: Special "not a number" value.
NEGATIVE_INFINITY: Special value representing negative infinity; returned on overflow.
POSITIVE_INFINITY: Special value representing infinity; returned on overflow.
prototype: Allows the addition of properties to a Number object.
[编辑] Methods
toExponential: Returns a string representing the number in exponential notation.
toFixed: Returns a string representing the number in fixed-point notation.
toLocaleString: Returns a human readable string representing the number using the locale of the environment. Overrides the Object.toLocaleString method.
toPrecision: Returns a string representing the number to a specified precision in fixed-point or exponential notation.
toSource: Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the Object.toSource method.
toString: Returns a string representing the specified object. Overrides the Object.toString method.
valueOf: Returns the primitive value of the specified object. Overrides the Object.valueOf method.
In addition, this object inherits the watch and unwatch methods from Object.
[编辑] Examples
[编辑] Example: Using the Number object to assign values to numeric variables
The following example uses the Number object's properties to assign values to several numeric variables:
biggestNum = Number.MAX_VALUE; smallestNum = Number.MIN_VALUE; infiniteNum = Number.POSITIVE_INFINITY; negInfiniteNum = Number.NEGATIVE_INFINITY; notANum = Number.NaN;
[编辑] Example: Using Number object to modify all Number objects
The following example creates a Number object, myNum, then adds a description property to all Number objects. Then a value is assigned to the myNum object's description property.
myNum = new Number(65); Number.prototype.description = null; myNum.description = "wind speed";