Semantics of "indexed" string access

Allen Wirfs-Brock Allen.Wirfs-Brock at
Tue Jun 24 17:46:26 PDT 2008

I've taken a crack at cleaning up Pratap's initial specification for supporting direct indexing of strings, eg "abc"[1] yields "b"

Here are the semantics that seemed to make sense:

1)  If s has an own property whose name is the same as the value of n, the value of that property is returned.
2) else If the value of n is convertible to a number that is within the bounds of the string value, return a string containing the corresponding character
3) else if try to resolve n as the name of an inherited property
4) else return undefined.

In other words, indexing into a string with a "valid" index returns that appropriate character unless somebody has explicitly defined a property named by that index on the object.
If the "index" is not within the bounds of the string it is treated like a normal property access - If it isn't defined either locally or on the parent chain the result is undefined.

I've specified this by "over-riding" the definition of [[GetOwnProperty]] for string instances".  Note that literal strings as in the example above will have been transformed into objects by the property access semantics of 11.2.1 [[GetOwnProperty]] (P)
String objects use a variation of the [[GetOwnProperty]] method used for other native ECMAScript objects (
Assume S is a String object and P is a string.
When the [[GetOwnProperty]] method of S is called with property name P, the following steps are taken:
1.      Call the default [[GetOwnProperty]] method ( with S as the this value and argument P.
2.      If Result(1) is not undefined return Result(1).
3.      If P is not an array index (15.4) , return undefined.
4.      Call ToString, giving S as its argument.
5.      Call ToInteger(P).
6.      Compute the number of characters in Result(4).
7.      If Result(5) is less than 0 or is not less than Result(4), return undefined.
8.      Return a string of length 1, containing one character from Result(4), namely the character at position Result(5), where the first (leftmost) character in Result(4) is considered to be at position 0, the next one at position 1, and so on.

Is this a reasonable semantics? Does it match what browsers have already implemented?
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Es4-discuss mailing list