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

Allen Wirfs-Brock allen at wirfs-brock.com
Fri Oct 18 10:30:31 PDT 2013


On Oct 18, 2013, at 10:06 AM, Andrea Giammarchi wrote:

> +1 for the simplified `at(symbolIndex)`
> 
> I would expect '𝌆'.at(1) to fail same way 'a'.charAt(1) or 'a'.charCodeAt(1) would.

They are comparable, as the 'a' example are "index out of bounds" errors. We only use code unit indices with strings so '𝌆'[1] is valid (and so presumably should be '𝌆'.at(1) with 1 having the same meaning in each case.

The most consistent way to define String.prototype.at be be:

String.prototype.at = function(pos} {
    let cp = this.codePointAt(pos);
    return cp===undefined ? undefined : String.fromCodePoint(cp)
}
   
Allen



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131018/eb6dce4e/attachment.html>


More information about the es-discuss mailing list