What Are Objects?
Apart from intrinsic objects, there are user-created objects (discussed below), and host objects.
Object properties and methods
You can access to object properties in the same way as if you were accessing an array:
By name, using the ObjectName.PropertyName syntax, or
By index, starting with 0.
// Car constructor
this.make = new Array();
this.colors = new Array();
this.price = new Array();
this.year = new Array();
this.model = new Array();
this.toString = toString; // Method (member function)
return ("Make: " + this.make + "\n" + "Color: " + this.colors + "\n" + "Year: " + this.year + "\n" + "Model: " + this.model + "\n" + "Price: " + this.price + "\n");
// Create an instance of a Car
BMW = new Car();
BMW.colors = "Blue";
BMW.price = "$50000";
BMW.year = 2006;
BMW.model = "Z4";
//There are two ways to refer to the object model.
MyModel = BMW.model;
MyModel2 = BMW["model"];
//Add more colors
BMW.colors = "Red";
BMW.colors = "Brown";
BMW.colors = "Black";
If your object's property is another object, use the dot-notation to access its properties:
// The substring method is the property of String object
The following code creates a multiplication table (as an array of arrays):
// Construct a 17x17 multiplication table
multTable = new Array(17);
for ( j = 0; j < multTable.length; j++)
// Create rows.
aRow = new Array(17);
for ( i = 0; i < aRow.length; i++)
aRow[i] = (i + " times " + j + " = " + i*j);
// Put the filled row into the table.
multTable[j] = aRow;
// Get only row 7 to get the multiplication table for this number
multiTable7 = multTable;
To refer to one of the elements of an array of this kind, use two sets of brackets.
multiply3x7 = multTable;