JavaScript/Notes/Array: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
(Created page with " 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 me...")
 
No edit summary
Line 7: Line 7:
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.
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.


<source lang="javascript">
Array.prototype.indexOf
Array.prototype.indexOf
Array.prototype.lastIndexOf
Array.prototype.lastIndexOf
Line 16: Line 17:
Array.prototype.reduce
Array.prototype.reduce
Array.prototype.reduceRight
Array.prototype.reduceRight
</source>


Here is the link to Array.prototype:
Here is the link to Array.prototype:

Revision as of 12:57, 29 October 2013


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.

<source lang="javascript"> 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 </source>

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.