Java Reference
In-Depth Information
Notice that a property name is considered an index if it can be converted to an
integer between 0 and 2 32 -2 (inclusive). The real test to check whether a property name
is an index is to apply the following condition. Suppose property name is prop and it is a
string. If the following expression returns true , the property name is an index; otherwise,
it is just a property name:
ToString(ToUint32(prop)) = prop
Here, assume that ToUint32() is function to convert the property name to an
unsigned 32-bit integer and ToString() is a function to convert the integer to a string. In
other words, if a string property name converted to an unsigned 32-bit integer and back
to a string results in the original property name, such a property name is an index. If the
property name is simply a number in the valid range, it is an index if does not contain a
fractional part. The following code demonstrates this rule:
// Create an array with two elements
var names = ["Fu", "Li"]
// Adds an element at the index 2
names[2.0] = "Su";
// Adds a property named "2.0", not an element at index 2
names["2.0"] = "Bo";
// Adds an element at index 3
names["3"] = "Do";
print("names.length = " + names.length);
// Print all properties of the array using a for..in loop
print("Using a for..in loop:");
for(var prop in names) {
print("names[" + prop + "] = " + names[prop]);
}
names.length = 4
Using a for..in loop:
names[0] = Fu
names[1] = Li
names[2] = Su
names[3] = Do
names[2.0] = Bo
Search WWH ::




Custom Search