mozilla

Revision 72640 of New in JavaScript 1.6

  • Revision slug: JavaScript/New_in_JavaScript/1.6
  • Revision title: New in JavaScript 1.6
  • Revision id: 72640
  • Created:
  • Creator: Wladimir_Palant
  • Is current revision? No
  • Comment /* E4X */ Added information on enabling E4X

Revision Content

E4X

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.

E4X syntax conflicts with the common practice of putting scripts into HTML comments thus hiding them from old browsers. For that reason you have to specify a different MIME type for scripts in your web application if you want E4X enabled:

<script type="text/javascript; e4x=1">
var xml = <root>
            <child1>value1</child1>
            <child2>value2</child2>
          </root>;

alert(xml.child1);
xml.child2 = 'something else';
alert(xml);
</script>

With the standard MIME type you can still use E4X but you will have to use the explicit XML constructor:

<script type="text/javascript">
var xml = new XML('<root><child /></root>');
alert(xml);
</script>

Note that E4X is always enabled for scripts in extensions.

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'));

Revision Source

<p>
</p>
<h3 name="E4X"> E4X </h3>
<p>ECMAScript for XML (<a href="Special:Tags?tag=E4X&amp;language=en">E4X</a>) is a powerful technology for creating and processing <a href="en/XML">XML</a> content within <a href="en/JavaScript">JavaScript</a>.  We're going to continue to improve our E4X support, including adding transparent integration with the existing <a href="en/DOM">DOM</a>, but developers who are building XML-based web applications can benefit from E4X support in Firefox 1.5.
</p><p>E4X syntax conflicts with the common practice of putting scripts into HTML comments thus hiding them from old browsers. For that reason you have to specify a different MIME type for scripts in your web application if you want E4X enabled:
</p>
<pre>&lt;script type="text/javascript; e4x=1"&gt;
var xml = &lt;root&gt;
            &lt;child1&gt;value1&lt;/child1&gt;
            &lt;child2&gt;value2&lt;/child2&gt;
          &lt;/root&gt;;

alert(xml.child1);
xml.child2 = 'something else';
alert(xml);
&lt;/script&gt;
</pre>
<p>With the standard MIME type you can still use E4X but you will have to use the explicit XML constructor:
</p>
<pre>&lt;script type="text/javascript"&gt;
var xml = new XML('&lt;root&gt;&lt;child /&gt;&lt;/root&gt;');
alert(xml);
&lt;/script&gt;
</pre>
<p>Note that E4X is always enabled for scripts in extensions.
</p>
<h3 name="Array_extras"> Array extras </h3>
<p>There are seven new <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></code> methods that can be separated into two categories, item location methods and iterative methods. The item location methods are:
</p>
<ul><li> <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf">indexOf()</a></code> - returns the index of the given item's first occurrence.
</li><li> <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf">lastIndexOf()</a></code> - returns the index of the given item's last occurrence.
</li></ul>
<p>The iterative methods are:
</p>
<ul><li> <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/every">every()</a></code> - runs a function on every item in the array and returns true if the function returns true for every item.
</li><li> <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/filter">filter()</a></code> - runs a function on every item in the array and returns an array of all items for which the function returns true.
</li><li> <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/forEach">forEach()</a></code> - runs a function on every item in the array.
</li><li> <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/map">map()</a></code> - runs a function on every item in the array and returns the results in an array.
</li><li> <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/some">some()</a></code> - runs a function on every item in the array and returns true if the function returns true for any one item.
</li></ul>
<p>For more information, see Nicholas C. Zakas' article, <span class="exlink"><a href="en/Mozilla's_New_Array_Methods_(external)">Mozilla's New Array Methods</a></span>.
</p>
<h3 name="Array_and_String_generics"> Array and String generics </h3>
<p>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 <var>str</var> is a letter, you would write:
</p>
<pre>function isLetter(character) {
  return (character &gt;= "a" &amp;&amp; character &lt;= "z");
}

if (Array.prototype.every.call(str, isLetter))
  alert("The string '" + str + "' contains only letters!");
</pre>
<p>This notation is rather wasteful and JavaScript 1.6 introduces a generic shorthand:
</p>
<pre>if (Array.every(str, isLetter))
  alert("The string '" + str + "' contains only letters!");
</pre>
<p>Similarly you can easily apply String methods to any object:
</p>
<pre>var num = 15;
alert(String.replace(num, /5/, '2'));
</pre>
Revert to this revision