Array Power, Part II - DHTML Lab | 3 | WebReference

Array Power, Part II - DHTML Lab | 3

Logo

Array Power, Part II
JavaScript 1.2 vs JavaScript 1.3


Netscape Navigator 4.0 shipped with the JavaScript 1.2 interpreter. It was JavaScript 1.2 that introduced the powerful Array methods we have been discussing.

With the standardization of JavaScript through ECMAScript, a few modifications needed to be made to the Navigator JavaScript engine for it to be compliant. One of these changes was to the push() method:

JavaScript 1.2JavaScript 1.3
the push() method returns the last element added to an arraythe push() method returns the new length of the array.
lastElement = myArray.push(newElement)arrayLength = myArray.push(newElement)

In our example, on the previous page, we used the push() behavior introduced in JavaScript 1.3. This is a much better, and certainly more logical behavior. Think about it. When we append an element to an array, we already know that element! We don't need to identify it through a return value! It's the array length that is updated and we should be aware of.

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 push() 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. Confusing?

It should be. If not confusing, then just the thought of having to identify browser versions or JavaScript version compatibility through browser-sniffing code or multiple SCRIPT statements, should be discouraging.

We'll simplify things. We'll need to 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.

To achieve the above, we need not to browser-sniff or script-sniff, but to array-sniff! We'll need to identify what behavior the browser supports for push(). If it is the old behavior, then we can redefine push() using prototyping.


Produced by Peter Belesis and

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

URL: http://www.webreference.com/dhtml/column32/3.html