Arrays in JScript are sparse. That is, if you have an array with three elements that are numbered 0, 1, and 2, you can create element 50 without worrying about elements 3 through 49. If the array has an automatic length variable (see Intrinsic Objects for an explanation of automatic monitoring of array length), the length variable is set to 51, rather than to 4. You can certainly create arrays in which there are no gaps in the numbering of elements, but you are not required to.
在Jscript里数组是稀疏的。那就是说,如果你有一个包含数字0,1,2这三个数字的数组,你可以创建一个50个元素的数组,不用担心3到49的元素。如果数组有一个自动长度变量(看Intrinsic Objects,有一个对于数组长度自动监视的解释),这个长度变量被设置为51,而不是4。当然你可以创建没有间隙的数组,这并不是必须的。
In JScript, objects and arrays are almost identical to each other. The two main differences are that normal objects do not have an automatic length property, and arrays do not have the properties and methods of an object.
在Jscript里,对象和数组有着相似的定义。两个主要的不同是一般对象不包括自动length属性,并且数组没有对象的属性和方法。
Addressing Arrays 数组的寻址
You address arrays by using brackets "[]". The brackets enclose either a numeric value, or an expression that evaluates to a whole number. The following example assumes that the entryNum variable is defined and assigned a value elsewhere in the script.
你可以使用“[]”对数组进行寻址。这个中括号包含任一数字直,或者一个可以计算得到一个整数的表达式。下面的例子假定entryNum变量已经在脚本的某个地方被定义并且赋值。
theListing = addressBook[entryNum];
theFirstLine = theListing[1];
Objects as Associative Arrays 对象可以作为一个联想的数组
Normally, you use the dot operator "." to access an object's properties.
正常的,你可以使用操作符“.”访问一个对象的属性。
For example,
myObject.aProperty
Here, the property name is an identifier. You can also access an object's properties using the index operator "[]". Here, you are treating the object as an associative array. An associative array is a data structure that allows you to dynamically associate arbitrary data values with arbitrary strings. For example,
这里,属性名是一个标识符。你也可以使用索引操作“[]”访问一个对象的属性.这样,你可以把对象当成一个联想的数组来处理。一个联想到数组是一个数据结构,它允许你使用任意字符串动态的访问任意数据.例如,
myObject["aProperty"] // Same as above.
Although the use of the index operator is more commonly associated with accessing array elements, when used with objects, the index is always the property name expressed as a string literal.
虽然访问数组元素索引操作符的使用更平常,但是当使用对象,索引总是一个表示字面意思的属性名。
Notice the important difference between the two ways of accessing object properties.
注意访问对象属性的两个方法的重要不同:
Operator
| The property name is treated as
| Meaning the property name
|
Dot "." | an identifier | cannot be manipulated as data |
Index "[]" | a string literal | can be manipulated as data |
This difference becomes useful when you do not know what the property names will be until runtime (for example, when you are constructing objects based on user input). To extract all the properties from an associative array, you must use the for … in loop.
直到运行时你不知道属性名是什么时,这个区别将很有用。可以析取所有的来自一个联想数组的属性,必须使用 for … in 循环
(我加上后面这段话的例子,容易理解)
<script language="jscript" type="text/jscript">
function test(a,b,c)
{
this.a = a;
this.b = b;
this.c = c;
}
var t = new test(1,2,3);
for(var o in t)
{
document.write(o+"<br>");
}
</script>