Proposal: Array.prototype.last()

Jordan Osete jordan.osete at
Tue May 11 15:03:54 PDT 2010

Sorry for this late answer.
Using a temporary for this is not very convenient, it basically forces you to create an useless variable just for this purpose.
The slice / splice solution is a little bit better, but i find it less clear, and more error prone.

Maybe the name "last" is not the best for this functionality, and also maybe it's not a good idea to use the same method for getting and setting (probably a jQuery habit). Still, indexing arrays from the end seems quite common for me.

What about some simple getItem( index ) / setItem( index, item ) variation ?
More language-like and more convenient than the current solutions, they could easily be defined with slice- and splice-compatible behaviour:

  Array.prototype.getItem = function (i) {return this.slice (i, 1)[0];};
  Array.prototype.setItem = function (i, v) {this.splice (index, 1, [v]);};


Jordan Osete

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list