Microsoft® JScript®
JScript Objects |
JScript Tutorial; Previous Next |
What Are Objects?
In Microsoft JScript, objects are, essentially, collections of properties and methods. A method is a function that is a member of an object, and a property is a value or set of values (in the form of an array or object) that is a member of an object. JScript supports three kinds of objects: intrinsic objects, objects you create, and browser objects, which are covered elsewhere.
Objects as Arrays
In JScript, objects and arrays are handled identically. You can refer to any of the members of an object (its properties and methods) either by name (using the name of the object, followed by a period, followed by the name of the property) or by its array subscript index. Subscript numbering in JScript begins with 0. For convenience, the subscript can also be referred to by its name.Thus, a property can be referred to in several ways. All of the following statements are equivalent.
While it is possible to use brackets to refer to a property by its numeric index, it is not possible to use the dot (.) convention with index numbers. The following statement generates an error.theWidth = spaghetti.width; theWidth = spaghetti[3]; // [3] is the "width" index. theWidth = spaghetti["width"];
When an object has another object as a property, the naming convention extends in a straightforward way.theWidth = spaghetti.3;
The fact that objects can have other objects as properties lets you generate arrays with more than one subscript, which are not directly supported. The following code creates a multiplication table for values from 0 times 0 through 16 times 16.var init4 = toDoToday.shoppingList[3].substring(0,1); // shoppingList, an array, is a property of toDoToday.
To refer to one of the elements of an array of this kind, use multiple sets of brackets.var multTable = new Array(17); // Make the shell that will become the table. for (var j = 0; j < multTable.length; j++) { // Prepare to fill it with rows. var aRow = new Array(17); // Create a row. for (var i = 0; i < aRow.length; i++) { // Prepare to fill the row. aRow[i] = (i + " times " + j + " = " + i*j); // Make and place one value. } multTable[j] = aRow; // Put the filled row into the table. }
The following statement generates an error.var multiply3x7 = multTable[3][7];
var multiply3x7 = multTable[3, 7];