Thursday, December 1st, 2005

New in JavaScript 1.6: E4X , Array Extensions, String generics

Category: JavaScript

Now Firefox 1.5 is out, Mozilla keeps moving. The Mozilla Developer Center wiki has posted a few nuggets on JavaScript 1.6.

E4X Info

ECMAScript for XML (E4X) is a powerful technology for creating and processing XML content within JavaScript. We’re going to continue to improve our E4X support, including adding transparent integration with the existing DOM, but developers who are building XML-based web applications can benefit from E4X support in Firefox 1.5.

You can still use the standard MIME type when using E4X:

<script type="text/javascript">

However, E4X syntax may conflict with the common practice of putting scripts into HTML comments (<!--...-->) to hide them from old browsers. E4X may also conflict with the more modern practice of putting scripts within XML CDATA sections (<[CDATA[...]]>) to allow the symbols “<” and “>” in the script (note that this does not apply to HTML). If you see inexplicable syntax errors, add “; e4x=1” to the MIME type:

<script type="text/javascript; e4x=1">

Note that scripts in extensions always treat HTML comments as E4X literals. That is, the “e4x=1” is implicit.

Array extras

There are seven new Array methods that can be separated into two categories, item location methods and iterative methods. The item location methods are:

  • indexOf() – returns the index of the given item’s first occurrence.
  • lastIndexOf() – returns the index of the given item’s last occurrence.

The iterative methods are:

  • every() – runs a function on every item in the array and returns true if the function returns true for every item.
  • filter() – runs a function on every item in the array and returns an array of all items for which the function returns true.
  • forEach() – runs a function on every item in the array.
  • map() – runs a function on every item in the array and returns the results in an array.
  • some() – runs a function on every item in the array and returns true if the function returns true for any one item.

For more information, see Nicholas C. Zakas’ article, Mozilla’s New Array Methods.

Array and String Generics

Sometimes you would like to apply array methods to strings, these treat a string as an array of characters. E.g. in order to check that every character in the variable str is a letter, you would write:

function isLetter(character) {
  return (character >= "a" && character <= "z");
}

if (Array.prototype.every.call(str, isLetter))
  alert("The string '" + str + "' contains only letters!");

This notation is rather wasteful and JavaScript 1.6 introduces a generic shorthand:

if (Array.every(str, isLetter))
  alert("The string '" + str + "' contains only letters!");

Similarly you can easily apply String methods to any object:

var num = 15;
alert(String.replace(num, /5/, '2'));

Posted by Dion Almaer at 3:31 pm
Comment here

+++--
3 rating from 5 votes

Comments Here »

Comments feed

Leave a comment

You must be logged in to post a comment.