Is this what you're thinking?

Array.prototype.nth = function (n){
  if(n < 0){
    return this[this.length -n];
  } else {
    return this[n];
  }
}

Thomas Foster

@thomasfoster96
Ph: +61477808008
http://thomasfoster.co/

> On 23 Jan 2016, at 4:40 PM, kdex <[email protected]> wrote:
> 
> While John's solution doesn't run into conflicts with downward compatibility, 
> it still wouldn't solve the problem of getting the n-th last element of an 
> array. To solve this, it'd probably be a good idea to extend the prototype 
> and 
> specify a parameter, defaulting to 1.
> 
> `Array.prototype.last` doesn't show up a terrible lot on search engines, 
> either, so we might actually be lucky here. Other than that, I also found two 
> more threads[1][2] on the EcmaScript discussion archives that propose it.
> 
> They might be worth a read.
> 
> [1] https://esdiscuss.org/topic/array-prototype-last
> [2] https://esdiscuss.org/topic/proposal-array-prototype-last
> 
>> On Samstag, 23. Januar 2016 15:44:19 CET John Gardner wrote:
>> Using a well-known symbol to access an array's last element is probably
>> wiser than a typical method or property:
>> 
>> let a = [0, 1, 2, 3];
>> console.log(
>> a[Symbol.last] === 3
>> /* true */
>> );
>> 
>> There're obviously instances where authors have extended Array prototypes
>> with "last" methods or properties, but we can't guarantee they'd all work
>> the same. For instance, assume there are some implementations that skip
>> undefined values:
>> 
>> var a = [0, 1, 2, undefined, undefined];
>> Array.prototype.last = function(){
>> return this[this.length - 1];
>> };
>> /** One that skips undefined values */
>> Array.prototype.last = function(){
>> var offset = 1;
>> while(offset < this.length && undefined === this[this.length - offset])
>> ++offset;
>> return this[this.length - offset];
>> }
>> 
>> These discrepancies are subtle, but have the potential to break backwards
>> compatibility.
>> 
>> Using a well-known symbol eliminates the potential for conflict.
>> Furthermore, it also offers an opportunity to complement any iterable
>> object's ability to synthesise array-like behaviour. For instance, it
>> enables iterables to also return the last object in their list of values:
>> 
>> let pruebas = {
>> data: ["Probando", "la", "mierda", "esta", undefined],
>> 
>> [Symbol.iterator](){
>> /* Stuff with .data */
>> },
>> [Symbol.last](){
>> /** Stuff to skip undefined values or whatever */
>> let offset = 1;
>> let data   = this.data;
>> while(
>> offset < data.length &&
>> undefined === data[data.length - offset]
>> )
>> ++offset;
>> return data[data.length - offset];
>> }
>> }
>> 
>> O
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to