JavaScript Language feature Idea

Michael Theriot michael.lee.theriot at gmail.com
Tue Apr 19 15:05:10 UTC 2016


>
> And Set by definition is an unordered collection, so there is no "last"
> element.


Sets in ES6 are iterated in insertion order.

I don't think a .get or .set needs to be made for Array. Why would you use
`arr.set(1, 4)` over `arr[1] = 4`, and why should there be more ways of
doing the same thing? It would only ever be used for negative indices, and
Reflect.get/Reflect.set already exists without that behavior.

The original question was to use negative indices, which you can do with
proxies. If a method should be added I think .last is the safest and would
expect any implementation of it to have the same behavior.

An `array[Symbol.last]` getter/setter would also be safe. You can even add
it yourself without collision.

```js
var last = Symbol();

Reflect.defineProperty(Array.prototype, last, {
  get: function () {
    return Reflect.get(this, this.length - 1);
  },
  set: function (value) {
    return Reflect.set(this, this.length - 1, value);
  }
});

var arr = [1,2,3];
arr[last] = 0;
console.log(arr); // 1,2,0
```

On Tue, Apr 19, 2016 at 9:37 AM, Michał Wadas <michalwadas at gmail.com> wrote:

>
> 2016-04-19 10:40 GMT+02:00 Jordan Harband <ljharb at gmail.com>:
>
>> (note that `Set#get(-1)` does not retrieve the last item in the set, for
>> example).
>>
>
>
> Because Set.prototype.get doesn't even exist. And Set by definition is an
> unordered collection, so there is no "last" element.
>
> I think Array.prototype.get and Array.prototype.set are the way to go - it
> should not break backward compatibility. And it's consistent with
> Array.prototype.slice behaviour.
>
>
> BTW, this was discussed 3 years ago:
> https://esdiscuss.org/topic/array-prototype-last
>
>
>
> _______________________________________________
> 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/cdf5b605/attachment.html>


More information about the es-discuss mailing list