Access Object Properties

You can access object properties by placing a period after the object name followed by the property name. For example, you can access the name property in the object stored in the variable named customer by using the following expression:

customer.name

An object property is a lot like a variable, that is, you can both write data to and read data from a property.

customer.name="John Doe";
say("Hello "+customer.name+".");

If you attempt to assign a value to a property that does not already exist, the property is automatically created. However, if you read a property that does not exist, the property is NOT created and the result null is returned.

var obj=new Object();    // create a new object
obj.x=12;                // create property x and set=12
var z=obj.y;             // z = null, y is not created

You can also access properties using a syntax similar to what is used to access list elements. The following two expressions are equivalent:

customer.name
customer["name"]

The advantage to the second syntax is you can choose the property name at run-time. For example,

var x="name";
customer[x]="John Doe";