Editing JavaScript/Notes/Array
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
In this lesson, I explain what an <code>Array</code> is, properties of Array instances, and methods of <code>Array.prototype</code> that were added in EcmaScript 5. | In this lesson, I explain what an <code>Array</code> is, properties of Array instances, and methods of <code>Array.prototype</code> that were added in EcmaScript 5. | ||
== | == Array Instances == | ||
Array instances also have a special | Array instances inherit properties from the <code>Array prototype</code> object and their <code>[[Class]]</code> internal property value is "Array". Array instances also have a special internal method called <code>[[DefineOwnProperty]] ( P, Desc, Throw )</code> used for property assignment and a special <code>length</code> property that affects and is affected by the properties in the array. | ||
<source lang="javascript"> | <source lang="javascript"> | ||
var a = ["a", "b" , "c", undefined]; | var a = ["a", "b" , "c", undefined]; | ||
"3" in a; // true; | "3" in a; // true; | ||
Line 12: | Line 8: | ||
a.length = 2; | a.length = 2; | ||
"2" in a; // false. | "2" in a; // false. | ||
</source> | </source> | ||
Line 31: | Line 17: | ||
— returns a live collection. | — returns a live collection. | ||
In modern browsers Array methods | In modern browsers in standards mode, it is possible to use Array methods generically on these Array-like objects. For example: | ||
<source lang="javascript"> | <source lang="javascript"> | ||
var slice = Array.prototype.slice; | var slice = Array.prototype.slice; | ||
var divs = | var divs = .getElementsByTagName("div"); | ||
divArray = slice.call(divs); // create a new Array | divArray = slice.call(divs); // create a new Array | ||
Line 46: | Line 32: | ||
divArray.filter(filterOutEmptyDivs); | divArray.filter(filterOutEmptyDivs); | ||
</source> | </source> | ||
== EcmaScript 5 Array Methods == | == EcmaScript 5 Array Methods == | ||
The | The [http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4 normative reference for Array is the EcmaScript 5.1 specification, a mature, official standard. | ||
EcmaScript 5 formally standardized the "Array Extras" that were previously introduced in Firefox 1.5 (Mozilla JavaScript™ 1.6) in 2006. | EcmaScript 5 formally standardized the "Array Extras" that were previously introduced in Firefox 1.5 (Mozilla JavaScript™ 1.6) in 2006. | ||
Line 72: | Line 52: | ||
</source> | </source> | ||
Each method description has an overview of what it does, followed by the algorithm. | Each method description has an overview of what it does, followed by the algorithm. Here is the link to Array.prototype: [http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4]. | ||
[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ | 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. | It is interesting that these methods can also be used generically on something that is Array-like. | ||
Line 88: | Line 69: | ||
== Assignment 1 Write a function that removes an element from an array. == | == Assignment 1 Write a function that removes an element from an array. == | ||
Write a function that | Write a function that removes an element from an array. | ||
== Assignment 2 Write a function that removes duplicate elements from an array. == | |||
== Assignment 3 Write a function that sorts an array of strings case-insensitively. == | == Assignment 3 Write a function that sorts an array of strings case-insensitively. == | ||
Write a function that sorts an array of strings case-insensitively. | Write a function that sorts an array of strings case-insensitively. | ||