`String.prototype.symbolAt()` (improved `String.prototype.charAt()`)

Mathias Bynens mathias at qiwi.be
Fri Oct 18 22:12:01 PDT 2013

On 18 Oct 2013, at 17:51, Joshua Bell <jsbell at google.com> wrote:

> Given that you can only use the proposed String.prototype.at() properly for indexes > 0 if you know the index of a non-BMP character or lead surrogate by some other means, or if you will test the return value for a trailing surrogate, is it really an advantage over using codePointAt / fromCodePoint?
> The name "at" is so tempting I'm imagining naive scripts of the form for (i = 0; i < s.length; ++i) { r += s.at(i); } which will work fine until they get a non-BMP input at which point they're suddenly duplicating the trailing surrogates.
> Pushing people towards for-of iteration and even Allen's Array.from( '𝌆𝌆𝌆'))[1] seems safer; users who need more subtle things have have codePointAt / fromCodePoint available and hopefully the knowledge to use them.

Just because new features can be used incorrectly doesn’t mean the feature isn’t useful. `for…of` on strings and `String.prototype.at` are two very different things for two very different use cases. It’s a matter of using the right tool for the job, IMHO.

In your example (iterating over all code points in a string), `for…of` should be used.

`String.prototype.codePointAt` or `String.prototype.at` come in handy in case you only need to get the first code point or symbol in a string, for example.

More information about the es-discuss mailing list