Array Power, Part III - DHTML Lab | 10 | WebReference

Array Power, Part III - DHTML Lab | 10

Logo

Array Power, Part III
JavaScript 1.2 vs JavaScript 1.3


Like the push() method, discussed in Array Power, Part II, the behavior of splice() in Navigator differs depending on the JavaScript interpreter used.

JavaScript 1.2JavaScript 1.3 (ECMAScript Compliant)
the splice() method returns an array of elements removed, if more than one elements are removed, and the element itself if only one element is removed. If no elements are removed, the return value is undefined the splice() method always returns an array. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
removedElementsArray = myArray.splice(2,3);
singleElement = myArray.splice(2,1);
undefinedValue = myArray.splice(2,0);
removedElementsArray = myArray.splice(2,3);
singleElementArray = myArray.splice(2,1);
emptyArray = myArray.splice(2,0);

In our examples, we have been using the splice() behavior introduced in JavaScript 1.3. Like the improvements in push(), this is a more logical behavior. When we assign the removed elements to a variable, we would need three different approaches to using the stored elements, not to mention the nightmare of undefined values.

JavaScript 1.3 shipped with Navigator versions 4.06, 4.07, 4.08, and 4.5+.

For backward compatibility, if you use the LANGUAGE="JavaScript1.2" attribute in a SCRIPT tag, new versions of Navigator will still assume splice() to have the old behavior. If you omit the LANGUAGE attribute, the new behavior kicks in. If LANGUAGE is set to "JavaScript1.3", the new behavior is used.

Like before, we'll assume the following:

The JavaScript 1.3 behavior is correct. We want this behavior to persist regardless of what version of Navigator we are using (even old ones) and regardless of what value we have given the LANGUAGE attribute.

Array sniffing

We create an array with a single element. The element is the number 0. This is as short an array as we can declare:

myArray = [0];

If we apply the splice() method to myArray, passing only one argument, 0, the method will remove all elements starting from index 0:

returnValue = myArray.splice(0);

Since there is only one element to remove, the return value will be a number (the element itself) for old interpreters and an array for new interpreters. If we check the type of return value, we can identify the interpreter:

myArray = [0];
oldInterpreter = typeof(myArray.splice(0))=="number";

To shorten the code, we can substitute an array literal for the array declaration:

oldInterpreter = typeof([0].splice(0))=="number";

Again, as we did with push(), we will check if splice() is supported. If it is, which behavior of splice() is supported. If the old behavior is supported, then we'll disable the old support:

if(Array.prototype.splice && typeof([0].splice(0))=="number")
    Array.prototype.splice = null;

Now, we can prototype splice() for both Explorer, all versions, and Navigator, when an old interpreter is used.


Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: May 16, 2000
Revised: May 16, 2000

URL: http://www.webreference.com/dhtml/column33/6.html