Section 3.14. Object-to-Primitive Conversion


3.14. Object-to-Primitive Conversion

Objects can usually be converted to primitive values in the straightforward way described in Section 3.5.3. Several details of this conversion require additional discussion, however.[*]

[*] This section covers advanced material, which you may want to skip on your first reading.

First, note that whenever a non-null object is used in a Boolean context, it converts to true. This is true for all objects (including arrays and functions), even for all wrapper objects that represent primitive values that convert to false. For example, all the following objects convert to TRue when used in a Boolean context:

 new Boolean(false)  // Internal value is false, but object converts to true new Number(0) new String("") new Array( ) 

Table 3-3 showed that objects are converted to numbers by first calling the valueOf( ) method of the object. Most objects inherit the default valueOf( ) method of Object, which simply returns the object itself. Since the default valueOf( ) method does not return a primitive value, JavaScript next tries to convert the object to a number by calling its toString( ) method and converting the resulting string to a number.

This leads to interesting results for arrays. The toString( ) method of arrays converts the array elements to strings, then returns the result of concatenating these strings, with commas in between. Therefore, an array with no elements converts to the empty string, which converts to the number zero! Also, if an array has a single element that is a number n, the array converts to a string representation of n, which is then converted back to n itself. If an array contains more than one element, or if its one element is not a number, the array converts to NaN.

<The conversion of a value depends on the context in which it is used. There are a couple of circumstances in JavaScript in which the context is ambiguous. The + operator and the comparison operators (<, <=, >, and >=) operate on both numbers and strings, so when an object is used with one of these operators, it is not clear whether it should be converted to a number or a string. In most cases, JavaScript first attempts to convert the object by calling its valueOf( ) method. If this method returns a primitive value (usually a number), that value is used. Often, however, valueOf( ) simply returns the unconverted object; in this case, JavaScript then tries to convert the object to a string by calling its toString( ) method.

But there is one exception to this conversion rule: when a Date object is used with the + operator, the conversion is performed with the toString( ) method. This exception exists because Date has both toString( ) and valueOf( ) methods. When a Date is used with +, you almost always want to perform a string concatenation. But when using a Date with the comparison operators, you almost always want to perform a numeric comparison to determine which of two times is earlier than the other.

Most objects either don't have valueOf( ) methods or don't have valueOf( ) methods that return useful results. When you use an object with the + operator, you usually get string concatenation rather than addition. When you use an object with a comparison operator, you usually get string comparison rather than numeric comparison.

An object that defines a custom valueOf( ) method may behave differently. If you define a valueOf( ) method that returns a number, you can use arithmetic and other operators with your object, but adding your object to a string may not achieve the desired result: the toString( ) method is no longer called, and a string representation of the number returned by valueOf( ) is concatenated to the string.

Finally, remember that valueOf( ) is not called toNumber( ); strictly speaking, its job is to convert an object to a reasonable primitive value, so some objects may have valueOf( ) methods that return strings.




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net