[Noisebridge WebDev] Array Methods in EcmaScript 5

Garrett Smith dhtmlkitchen at gmail.com
Tue Oct 29 05:41:42 UTC 2013


Hey NB Webdev -

Array methods are used or hand-rolled (as a fallback in a lot of javascript
libraries, such as jQuery and Underscore, as discussed tonight.

EcmaScript 5 has standardized methods that were introduced as "Array
Extras" in Firefox 1.5. These neat methods are present in all modern
browsers, but can have a noticeable performance impact for lengthy arrays,
particularly on limited devices.

The normative reference for these methods is the EcmaScript 5.1
specification, a mature, official standard. Each method description has an
overview of what it does, followed by the algorithm.

Array.prototype.indexOf
Array.prototype.lastIndexOf
Array.prototype.every
Array.prototype.some
Array.prototype.forEach
Array.prototype.map
Array.prototype.filter
Array.prototype.reduce
Array.prototype.reduceRight

Here is the link to Array.prototype:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4

MDC on Array:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

It is interesting that these methods can also be used *generically* on
something that is *Array-like*.

var filter = Array.prototype.filter;

filter.call("foo",  function (ch) { return ch == "o"; }).join("");

Array-like was proposed as a standard interface (I championed this idea for
a while), but was not included. Perhaps it will be, some day.

But for now, it is useful to know that these methods are natively supported
in modern browsers.

In a few weeks from now, I will be teaching a class on javascript to delve
into a deeper understanding of the EcmaScript programming language itself,
as well as coding strategies and reuse patterns.

Regards,
-- 
Garrett
@xkit
ChordCycles.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.noisebridge.net/pipermail/webdev/attachments/20131028/9e5efeb4/attachment-0002.html>


More information about the WebDev mailing list