JavaScript/Notes/Array

From Noisebridge
< JavaScript(Difference between revisions)
Jump to: navigation, search
Line 26: Line 26:
  
 
It is interesting that these methods can also be used generically on something that is Array-like.
 
It is interesting that these methods can also be used generically on something that is Array-like.
 
+
<source lang="javascript">
 
var filter = Array.prototype.filter;
 
var filter = Array.prototype.filter;
  
 
filter.call("foo",  function (ch) { return ch == "o"; }).join("");
 
filter.call("foo",  function (ch) { return ch == "o"; }).join("");
 +
</source>
  
 
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.
 
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.
 
But for now, it is useful to know that these methods are natively supported in modern browsers.
 +
== Assignment 1 ==
 +
Write a function that removes an element from an array.
  
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.
+
== Assignment 2 ==
 +
Write a function that removes duplicate elements from an array.

Revision as of 13:04, 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.

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.

Assignment 1

Write a function that removes an element from an array.

Assignment 2

Write a function that removes duplicate elements from an array.

Personal tools