Team LiB   Previous Section   Next Section

11.6 Netscape's JavaScript 1.2 Incompatibilities

Netscape's implementation of JavaScript 1.2 was released (as part of the Netscape 4.0 browser) while the ECMAScript v1 specification was still being finalized. The engineers at Netscape made some guesses about what would be in the specification, and based on those guesses, they made some changes to the way JavaScript behaved. Because these changes were not compatible with previous versions of JavaScript, the changes were implemented only when JavaScript 1.2 was explicitly requested. (In web browsers, this is done by setting the language attribute of the HTML <script> tag to "JavaScript1.2".) This was an excellent way to introduce new behavior without breaking old scripts. Unfortunately, when work on ECMAScript v1 was completed, the new behavior that Netscape engineers had guessed at was not part of the standard. What this means is that Netscape's implementation of JavaScript 1.2 has special-case behavior that is not compatible with JavaScript 1.1 and does not conform to the ECMAScript specification.

For compatibility with scripts that rely on the nonconforming behavior of JavaScript 1.2, all future implementations of JavaScript from Netscape have retained this special behavior when Version 1.2 is explicitly requested. Note, however, that if you request a version greater than 1.2 (with a language attribute of "JavaScript1.3", for example) you will get ECMAScript-compliant behavior. Because this special behavior is present only in JavaScript implementations from Netscape, you should not rely on it in your scripts, and the best practice is to never explicitly specify Version 1.2. Nevertheless, for those cases when you must use JavaScript 1.2, the special behaviors of that version are listed here:

  • The equality and inequality operators behave like the identity and non-identity operators. That is, == works like === and != works like !==.

  • The default Object.toString( ) method displays the values of all properties defined by the object, returning a string formatted using object literal syntax.

  • The Array.toString( ) method separates array elements with a comma and a space, instead of just a comma, and returns the list of elements within square brackets. In addition, string elements of the array are quoted, so that the result is a string in legal array literal syntax.

  • When a single numeric argument n is passed to the Array( ) constructor, it returns an array with n as its single element, rather than an array of length n.

  • When an array object is used in a numeric context, it evaluates to its length. When used in a boolean context, it evaluates to false if its length is 0 and otherwise evaluates to true.

  • The Array.push( ) method returns the last value pushed rather than the new array length.

  • When the Array.splice( ) method splices out a single element x, it returns x itself, rather than an array containing x as its only element. When splice( ) does not remove any elements from the array, it returns nothing instead of returning an empty array.

  • When String.substring( ) is called with a starting position greater than its ending position, it returns the empty string rather than correctly swapping the arguments and returning the substring between them.

  • The String.split( ) method displays special behavior inherited from Perl: if the specified separator character is a single space, it discards any leading and trailing whitespace in the string before splitting the remainder of the string.

    Team LiB   Previous Section   Next Section