Proposal: Array.prototype.first() and Array.prototype.last()

Bob Myers rtm at gol.com
Tue Sep 27 12:38:21 UTC 2016


This is well-traveled territory.

Whatever is or is not implemented, interfaces which have optional arguments
and return scalars in one case and arrays in another case are a horrible
idea.

To my knowledge no-one has ever explained why the following is a bad idea:

```
array.0
array.-1
```

Bob

On Tue, Sep 27, 2016 at 5:42 PM, Nicu Micleusanu <micnic90 at gmail.com> wrote:

> I propose to standardize `Array.prototype.first()` and
> `Array.prototype.last()`, very similar to underscore `_.first()` and
> `_.last()`.
>
>
> A very basic implementation:
>
> ```js
>
> Array.prototype.first = function (n) {
>
>     if (!arguments.length) {
>
>         return this[0];
>
>     } else {
>
>         return this.slice(0, Math.max(0, n));
>
>     }
>
> };
>
>
> Array.prototype.last = function (n) {
>
>     if (!arguments.length) {
>
>         return this[this.length - 1];
>
>     } else {
>
>         return this.slice(Math.max(0, this.length - n));
>
>     }
>
> };
>
> ```
>
> _______________________________________________
> 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/20160927/5bf2022d/attachment.html>


More information about the es-discuss mailing list