• No results found

A – Array object (Object/core)

Example code:

<SCRIPT>

// Create a simple array literal

var myArray = [ 100, 1.34, "String text", true, { prop:100 } ]; // Create a nested multi-dimensional array

var matarray = [ [1,0], [0,1]]; // JavaScript expression in arrays

var exprarray = [ Math.random()*10, Math.random()*100 ]; // Sparse array

var sparse = [100, , , , , 1000];

document.write(myArray[2] + "<BR>" + matarray[0,1] + "<BR>" + exprarray[1] + "<BR>" + sparse[5]);

</SCRIPT>

See also: Array object

Cross-references:

ECMA 262 edition 3 – section – 11.1.4

O'Reilly JavaScript, The Definitive Guide ISBN 1-56592-392-8– page – 46

Array object (Object/core)

An object of the class "Array".

Availability: ECMAScript edition – 2 JavaScript – 1.1

JScript – 3.0

Internet Explorer – 4.0 Netscape – 3.0

Netscape Enterprise Server – 2.0 Opera – 3.0

- myArray = Array

- myArray = myVBArray.toArray()

- myArray = new Array()

- myArray = new Array(aLength)

JavaScript syntax:

- myArray = new Array(anItem1, anItem2, anItem3, ...) aLength An optional initial length to set the array to.

Argument list:

anItemN A variable number of initial elements to insert into the array.

Object properties: constructor, index, input, length, prototype

Object methods: concat(), join(), pop(), push(), reverse(), shift(), slice(),

sort(), splice(), toLocaleString(), toSource(), toString(),

unshift(), valueOf()

In JavaScript version 1.0, arrays were simple objects and had limited functionality, scarcely enough really to be called arrays. Some commentators argue that the functionality was so limited that they should be flagged as available from version 1.1 of JavaScript only. They were usually simulated by creating an instance of the Object object and using its named properties as if the object was an array.

Much additional functionality was added for JavaScript version 1.1. JavaScript version 1.0 lacked the constructors and arrays had no special methods available. The ECMA standard enhances the functionality and Netscape 4 provides additional functionality.

An instance of the class "Array" is created by using the new operator on the Array() constructor. At JavaScript version 1.2, arrays can be created with an Array literal as well. The new object adopts the behavior of the built-in prototype object through the prototype-inheritance mechanisms. All properties and methods of the prototype are available as if they were part of the instance. Note that the index and input properties are available only for arrays that are produced as the result of a RegExp match. They are not generally available in Arrays or Collections.

An array is a collection of properties owned by an object and that can be accessed by name or by index position in the array. Because they are collected together and accessible as a set, they may be sorted into the order of the array.

Array objects give special treatment to property names, which are numeric values. These are used as an index value and will affect the value of the length property. The length supported depends on the platform, but is usually based on a 32 bit integer being used for addressing. That limits the range to 4 Billion array elements.

Array objects implement the Put() internal function slightly differently from non-array based objects. The prototype for the Array prototype object is the Object prototype object.

In the C language, an array is referred to as an aggregate type since it is made from a collection or aggregate of individual members.

Warnings:

❑ Although arrays were partially supported prior to JavaScript version 1.1, the support was not reliably or completely implemented. There was no way for the script developer to create and modify the arrays. Netscape 2 lacks any realistic array support even though Array objects were returned by some object properties.

❑ The WebTV set top box limits the extent of the Array objects to contain only 32,768 elements instead of the 4 Billion or so that is defined as the normal maximum. This is because WebTV uses 16 bit integers for addressing arrays rather than 32 bit integers.

A – Array object (Object/core)

Example code:

<SCRIPT>

// Array object demonstration var weekly_summary = new Array(7); weekly_summary[1] = 10;

weekly_summary[2] = 25;

var day_names = new Array("Su","Mo","Tu","We","Th","Fr","Sa"); for(var i=0; i<7; i++)

{

document.write("Summary for day ("); document.write(day_names[i]); document.write(") = "); document.write(weekly_summary[i]); document.write("<BR>"); } </SCRIPT>

See also: Aggregate type, Array index delimiter ([ ]), Array literal, Array(), Array(),

Array.Class, Array.length, Array.prototype, Collection object,

JavaArray object, JellyScript, Native object, Object object,

String.split(), unwatch(), VBArray.toArray(), watch()

Property JavaScript JScript N IE Opera NES ECMA Notes

constructor 1.1 + 3.0 + 3.0 + 4.0 + - - 2 + - index 1.2 + 5.5 + 4.0 + 5.5 + - - - - input 1.2 + 5.5 + 4.0 + 5.5 + - - - - length 1.0 + 3.0 + 2.0 + 4.0 + - - - ReadOnly prototype 1.1 + 3.0 + 3.0 + 4.0 + - - 2 + ReadOnly, DontDelete, DontEnum

Method JavaScript JScript N IE Opera NES ECMA Notes

concat() 1.2 + 3.0 + 4.0 + 4.0 + - 3.0 + 3 + Warning join() 1.1 + 3.0 + 3.0 + 4.0 + 3.0 + 2.0 + 2 + - pop() 1.2 + 5.5 + 4.0 + 5.5 + - 3.0 + 3 + - push() 1.2 + 5.5 + 4.0 + 5.5 + - 3.0 + 3 + - reverse() 1.1 + 3.0 + 3.0 + 4.0 + 3.0 + 2.0 + 2 + - shift() 1.2 + 5.5 + 4.0 + 5.5 + - 3.0 + 3 + - slice() 1.2 + 3.0 + 4.0 + 4.0 + - 3.0 + 3 + Warning sort() 1.1 + 3.0 + 3.0 + 4.0 + 3.0 + 2.0 + 2 + Warning splice() 1.2 + 5.5 + 4.0 + 5.5 + - 3.0 + 3 + Warning toLocaleString() 1.5 + 5.5 + 6.0 + 5.5 + - - 3 + Warning toSource() 1.3 + 3.0 + 4.06 + 4.0 + - - - - toString() 1.1 + 3.0 + 3.0 + 4.0 + 3.0 + 2.0 + 2 + Warning unshift() 1.2 + 5.5 + 4.0 + 5.5 + - 3.0 + 3 + - valueOf() 1.1 + 3.0 + 3.0 + 4.0 + - - - -

Cross-references:

ECMA 262 edition 2 – section – 8.6.2.2 ECMA 262 edition 2 – section – 15.4 ECMA 262 edition 3 – section – 8.6.2.2 ECMA 262 edition 3 – section – 15.4

Wrox Instant JavaScript ISBN 1-861001-27-4 – page – 15

Array() (Constructor)

An Array object constructor.

Availability: ECMAScript edition – 2 JavaScript – 1.1

JScript – 3.0

Internet Explorer – 4.0 Netscape – 3.0 Property/method value type: Array object

- new Array()

- new Array(aLength)

JavaScript syntax:

- new Array(anItem1, anItem2, anItem3, ...) aLength An optional initial length to set the array to

Argument list:

anItemN A variable number of initial elements to insert into

the array

The Array() constructor is used in a new expression to manufacture a new instance of the Array object.

The arguments passed to the constructor affect the way that the array is initialized.

If no arguments are passed, then an empty array is created. Its length will be zero and it will only have the properties it inherits from its prototype parent.

If it has a single argument, and if that argument is a numeric value that can be realized as an unsigned 32-bit integer with no loss of precision, then it is taken as a length value to initialize the array with. However, according the ECMA standard, a numeric value that is not convertible to a Uint32 should cause a run-time error. This may not be the case with all host implementations and would be considered a minor deviation from the standard. You may find that a single numeric value results in a one-element array containing that value instead of a run-time error. A single argument of non-numeric type results in an array containing one element and having a length value of 1.

If there is more than one argument, then each argument is placed into the array in the order of presentation and the length value set according to the number of arguments provided.

Related documents