Section 7.2. Object Properties


7.2. Object Properties

You normally use the . operator to access the value of an object's properties. The value on the left of the . should be the object whose property you want to access. Usually, this is just the name of the variable that contains the object reference, but it can be any JavaScript expression that evaluates to an object. The value on the right of the . should be the name of the property. This must be an identifier, not a string or an expression. For example, you would refer to the property p in object o with o.p or to the property radius in the object circle with circle.radius. Object properties work like variables: you can store values in them and read values from them. For example:

 // Create an object. Store a reference to it in a variable. var book = {}; // Set a property in the object. book.title = "JavaScript: The Definitive Guide" // Set some more properties. Note the nested objects. book.chapter1 = new Object(); book.chapter1.title = "Introduction to JavaScript"; book.chapter1.pages = 11; book.chapter2 = { title: "Lexical Structure", pages: 6 }; // Read some property values from the object. alert("Outline: " + book.title + "\n\t" +       "Chapter 1 " + book.chapter1.title + "\n\t" +       "Chapter 2 " + book.chapter2.title); 

An important point to notice about this example is that you can create a new property of an object simply by assigning a value to it. Although you declare variables with the var keyword, there is no need (and no way) to do so with object properties. Furthermore, once you have created an object property by assigning a value to it, you can change the value of the property at any time simply by assigning a new value:

 book.title = "JavaScript: The Rhino Book" 

7.2.1. Enumerating Properties

The for/in loop discussed in Chapter 6 provides a way to loop through, or enumerate, the properties of an object. This can be useful when debugging scripts or when working with objects that may have arbitrary properties whose names you do not know in advance. The following code shows a function you can use to list the property names of an object:

 function DisplayPropertyNames(obj) {     var names = "";     for(var name in obj) names += name + "\n";     alert(names); } 

Note that the for/in loop does not enumerate properties in any specific order, and although it enumerates all user-defined properties, it does not enumerate certain predefined properties or methods.

7.2.2. Checking Property Existence

The in operator (see Chapter 5) can be used to test for the existence of a property. The left side of this operator should be the name of the property as a string. The right side should be the object to be tested. For example:

 // If o has a property named "x", then set it if ("x" in o) o.x = 1; 

The in operator is not often needed, however, because if you query a property that does not exist, the undefined value is returned. Thus, the above code can usually be written like this:

 // If the property x exists and is not undefined, set it. if (o.x !== undefined) o.x = 1; 

Note that it is possible for a property to exist but still be undefined. For example, if you write:

 o.x = undefined 

the property x exists but has no value. In this situation, the first line of code in the previous paragraph sets x to 1, but the second line of code does nothing.

Note also that the !== operator was used earlier instead of the more common != operator. !== and === distinguish between undefined and null. Sometimes, however, you don't want to make this distinction:

 // If the doSomething property exists and is not null or undefined, // then assume it is a function and invoke it! if (o.doSomething) o.doSomething(); 

7.2.3. Deleting Properties

You can use the delete operator to delete a property of an object:

 delete book.chapter2; 

Note that deleting a property does not merely set the property to undefined; it actually removes the property from the object. After deletion, for/in will not enumerate the property and the in operator will not detect it.




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