JavaScript 1.3 Overview, Part II: Changes to the Array Object - Doc JavaScript | WebReference

JavaScript 1.3 Overview, Part II: Changes to the Array Object - Doc JavaScript


Changes to the Array Object

JavaScript 1.3 introduces a number of significant changes to the Array object. Read this page carefully, as there is a good chance you have to modify your code to adapt it to JavaScript 1.3.

The length property of the Array object is now an unsigned, 32-bit integer. Its value is a positive integer and it is less than 2 to the power of 32. The length property in JavaScript 1.2 is a signed integer with value less than 2 to the power of 31.

JavaScript 1.3 reverts to the JavaScript 1.1's array constructing specification. The Array constructor in JavaScript 1.2 constructs a single-element array when you specify a single parameter. When you specify ar = new Array(11) and Language="JavaScript1.2", you will get the single-element array, ar[0] = 11;. In JavaScript 1.3, though, a single-parameter constructor verifies that the argument is a number, converts it to an unsigned, 32-bit integer, and generates an array of the specified size. The length property of the array will be equal to the argument. The initial value of the Array elements will be undefined. Referring back to the example above, when you specify ar = new Array(11) and Language="JavaScript1.3", a new ar array will be generated, with 11 undefined elements. So, to summarize, the following two-line script:

a = new Array(11);
document.write("First element is " + a[0]);

will yield First element is undefined in JavaScript 1.3, First element is 11 in JavaScript 1.2, and First element is undefined in JavaScript 1.1.

The push() method behaves differently in JavaScript 1.3. In JavaScript 1.2, it returns the last element added to the array. In JavaScript 1.3, the push() method returns the new length of the array. The splice() method also changed. In JavaScript 1.2, it returns the element removed, if only one element is removed, and none, if more than one element is removed. In JavaScript 1.3, splice() always returns an array containing the removed elements. If one element is removed, a single-element array is returned.

http://www.internet.com


Created: September 28, 1998
Revised: September 28, 1998

URL: http://www.webreference.com/js/column26/array.html