Microsoft® JScript Objects |
| Tutorial |
|
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. For example, SQRT2 (a number, an approximation of the square root of 2) is a property of the Math object, and substring() (a function) is a method of the string object.
In JScript, objects and arrays are handled identically. You can refer to any of the constituents 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.
theWidth = spaghetti.width; theWidth = spaghetti[3]; // [3] is the "width" index. theWidth = spaghetti["width"];While it is possible to use brackets to refer to a property by its numeric index, it is not possible to use the "." convention with index numbers. The following statement generates an error.
theWidth = spaghetti.3;When an object has another object as a property, the naming convention extends in a straightforward way.
var init4 = toDoToday.shoppingList[3].substring(0,1); // shoppingList, an array, is a property of toDoToday.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 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. }To refer to one of the elements of an array of this kind, use multiple sets of brackets.var multiply3x7 = multTable[3][7];The following statement generates an error.var multiply3x7 = multTable[3, 7];
© 1996 by Microsoft Corporation.
file: /Techref/language/asp/js/310.htm, 3KB, , updated: 1996/11/22 10:12, local time: 2024/12/18 23:24,
18.224.43.232:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/language/asp/js/310.htm"> Microsoft® JScript Language Tutorial </A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.