Character indexing on strings

Jeff Walden jwalden at MIT.EDU
Wed Jun 13 17:47:33 PDT 2007


As described in the exported wiki right now, indexing on strings is equivalent to calling String.prototype.charAt on the string.  charAt returns "" for out-of-bounds values if I remember right, but it'd be nice if for negative values the index were calculated from the end of the string, as will occur with slice syntax (on strings, I might add), basically like so:

function index(str, ix)
{
  if (ix < 0)
    ix += str.length;
  return str.charAt(i);
}

The resultant inequality of charAt and indexing isn't ideal, but I think syntactic similarity makes it more important that slice notation and index notation be consistent.

(For completeness I note that str[-1] == str.length in SpiderMonkey, but I don't know of anyone else who implements this, and the current proposal contradicts this anyway.  :-) )

Jeff



More information about the Es4-discuss mailing list