JavaScript/Notes/Array

From Noisebridge
< JavaScript
Revision as of 12:57, 29 October 2013 by Garrett (Talk | contribs)

Jump to: navigation, search


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.

Personal tools