JavaScript Language feature Idea

Jordan Harband ljharb at gmail.com
Tue Apr 19 08:40:08 UTC 2016


Let's not cry doomsday because there's not an easy path to adding "-1"
array access, when indexing into a list of things *in the first place* is
already a potential code smell.

`endsWith` was able to be added because *it didn't break anything*.
`contains` was renamed to `includes` because the former *did* break
something. There's lots of examples of this principle at work. New things
can be added any time they don't break the web. Nothing will ever be
knowingly added that does break the web. Please accept that. Creating a new
web language won't necessarily "solve" this "problem" - future iterations
of *that* language would almost certainly still be subject to the
overriding principle of not breaking the web.

I think Array#get/Array#set are the most reasonable suggestions offered so
far, but I don't think the added value will be enough to convince the
committee that it's worth adding to the already large API surface of the
language - and the asymmetry with the similar methods on `Map`/`Set` would
be problematic (note that `Set#get(-1)` does not retrieve the last item in
the set, for example).

On Tue, Apr 19, 2016 at 1:17 AM, Tiddo Langerak <tiddolangerak at gmail.com>
wrote:

> If we're talking about adding a new method anyway, why not just add a more
> general `get` method with support for both positive and negative indices?
> That would be consistent with the other array methods, and additionally I
> think it is more natural to use `arr.get(-2)` than `arr.last(1)`. Using
> `last` to get anything but the very last element just seems weird to me.
>
> On 04/19/2016 10:00 AM, Bruno Jouhier wrote:
>
> Adding `last` to `Array.prototype` is no different than adding `endsWith`
> to `String.prototype`. ES2015 did it.
>
> It won't break most existing code that defines `Array.prototype.last`.
> Such code will just overwrite the new `last` method and will continue to
> work as before.
>
> You won't be able to mix old libraries that have their own `last` method
> with new code if the methods behave differently but that's a different
> story; that's not breaking existing web pages.
>
> What will break though is existing code that test the existence of
> `Array.prototype.last`; but `String.prototype.endsWith` had the same issue.
>
> last(1) would return last but one, and last() would be the same as last(0)
>
>
> _______________________________________________
> es-discuss mailing listes-discuss at mozilla.orghttps://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160419/76312a23/attachment.html>


More information about the es-discuss mailing list