Section 7.4. Universal Object Properties and Methods


7.4. Universal Object Properties and Methods

As discussed earlier, all objects in JavaScript inherit from the Object class. While more specialized categories of objects, such as those created with the Date() and RegExp() constructors define properties and methods of their own, all objects, however created, also support the properties and methods defined by Object. Because of their universality, these properties and methods are of particular interest.

7.4.1. The constructor Property

In JavaScript, every object has a constructor property that refers to the constructor function that initializes the object. For example, if I create an object d with the Date() constructor, the property d.constructor refers to Date:

 var d = new Date(); d.constructor == Date;  // Evaluates to true 

Since constructor functions define new categories or classes of objects, the constructor property can help determine the type of an object. For example, you might use code like the following to determine the type of an unknown value:

 if ((typeof o == "object") && (o.constructor == Date))     // Then do something with the Date object... 

The instanceof operator checks the value of the constructor property, so the code above could also be written:

 if ((typeof o == "object") && (o instanceof Date))     // Then do something with the Date object... 

7.4.2. The toString() Method

The toString() method takes no arguments; it returns a string that somehow represents the value of the object on which it is invoked. JavaScript invokes this method of an object whenever it needs to convert the object to a string. This occurs, for example, when you use the + operator to concatenate a string with an object or when you pass an object to a method such as alert() that expects a string.

The default toString() method is not very informative. For example, the following lines of code simply evaluate to the string "[object Object]":

 var s = { x:1, y:1 }.toString(); 

Because this default method does not display much useful information, many classes define their own versions of toString(). For example, when an array is converted to a string, you obtain a list of the array elements, themselves each converted to a string, and when a function is converted to a string, you obtain the source code for the function.

Chapter 9 describes how to define a custom toString() method for your own object types.

7.4.3. The toLocaleString() Method

In ECMAScript v3 and JavaScript 1.5, the Object class defines a toLocaleString() method in addition to its toString() method. The purpose of this method is to return a localized string representation of the object. The default toLocaleString() method defined by Object doesn't do any localization itself; it always returns exactly the same thing as toString(). Subclasses, however, may define their own versions of toLocaleString(). In ECMAScript v3, the Array, Date, and Number classes do define toLocaleString() methods that return localized values.

7.4.4. The valueOf() Method

The valueOf() method is much like the toString() method, but it is called when JavaScript needs to convert an object to some primitive type other than a stringtypically, a number. JavaScript calls this method automatically if an object is used in a context where a primitive value is required. The default valueOf() method does nothing interesting, but some of the built-in categories of objects define their own valueOf() method (see Date.valueOf(), for example). Chapter 9 explains how to define a valueOf() method for custom object types you define.

7.4.5. The hasOwnProperty() Method

The hasOwnProperty() method returns true if the object locally defines a noninherited property with the name specified by the single string argument. Otherwise, it returns false. For example:

 var o = {}; o.hasOwnProperty("undef");     // false: the property is not defined o.hasOwnProperty("toString");  // false: toString is an inherited property Math.hasOwnProperty("cos");    // true: the Math object has a cos property 

Property inheritance is explained in Chapter 9.

This method is defined in ECMAScript v3 and implemented in JavaScript 1.5 and later.

7.4.6. The propertyIsEnumerable() Method

The propertyIsEnumerable() method returns true if the object defines a noninherited property with the name specified by the single string argument to the method and if that property would be enumerated by a for/in loop. Otherwise, it returns false. For example:

 var o = { x:1 }; o.propertyIsEnumerable("x");        // true: property exists and is enumerable o.propertyIsEnumerable("y");        // false: property doesn't exist o.propertyIsEnumerable("valueOf");  // false: property is inherited 

This method is defined in ECMAScript v3 and implemented in JavaScript 1.5 and later.

Note that all user-defined properties of an object are enumerable. Nonenumerable properties are typically inherited properties (see Chapter 9 for a discussion of property inheritance), so this method almost always returns the same result as hasOwnProperty().

7.4.7. The isPrototypeOf() Method

The isPrototypeOf() method returns true if the object to which the method is attached is the prototype object of the argument. Otherwise, it returns false. For example:

 var o = {} Object.prototype.isPrototypeOf(o);        // true: o.constructor == Object Object.isPrototypeOf(o);                  // false o.isPrototypeOf(Object.prototype);        // false Function.prototype.isPrototypeOf(Object); // true: Object.constructor==Function 

Prototype methods are documented in Chapter 9.




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