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

Mathias Bynens mathias at qiwi.be
Fri Oct 18 13:30:20 PDT 2013


Please ignore my previous email; it has been answered already. (It was a draft I wrote up this morning before I lost my internet connection.)

On 18 Oct 2013, at 11:57, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:

> Given that we have charAt, charCodeAt and codePointAt,  I think the most appropiate name for such a method would be 'at':
>      '𝌆'.at(0)

Love it!

> The issue when this sort of method has been discussed in the past has been what to do when you index at a trailing surrogate possition:
> 
>     '𝌆'.at(1)
> 
> do you still get '𝌆' or do you get the equivalent of String.fromCharCode('𝌆'[1]) ?

In my proposal it would return the equivalent of `String.fromCharCode('𝌆'[1])`. I think that’s the most sane behavior in that case. This also mimics the way `String.codePointAt` works in such a case.

Here’s a prollyfill for `String.prototype.at` based on my earlier proposal: https://github.com/mathiasbynens/String.prototype.at Tests: https://github.com/mathiasbynens/String.prototype.at/blob/master/tests/tests.js


More information about the es-discuss mailing list