New in JavaScript 1.6

  • Revision slug: JavaScript/New_in_JavaScript/1.6
  • Revision title: New in JavaScript 1.6
  • Revision id: 72664
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment /* Array extras */

Revision Content

JavaScript 1.6 introduces several new features: E4X, several new Array methods, and Array and String generics.

JavaScript 1.6 is supported in Firefox 1.5 and later.

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.

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{{mediawiki.external('...')}}]>) 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.

E4X is documented in Processing XML with E4X.

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 items in the array while that function is returning true. It returns true if the function returns true for every item it could visit.
  • 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 items in the array while that function returns false. It returns true if the function returns true for any item it could visit.

For more information see Working with Arrays, or consult Nicholas C. Zakas' article, Mozilla's New Array Methods.

Array and String generics

Sometimes you would like to apply array methods to strings. By doing this, you treat a string as an array of characters. For example, 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'));

See also Working with Array-like objects.

{{ wiki.languages( { "ru": "ru/\u041d\u043e\u0432\u043e\u0435_\u0432_JavaScript_1.6", "es": "es/Novedades_en_JavaScript_1.6", "fr": "fr/Nouveaut\u00e9s_dans_JavaScript_1.6", "ja": "ja/New_in_JavaScript_1.6", "pl": "pl/Nowo\u015bci_w_JavaScript_1.6", "zh-cn": "cn/New_in_JavaScript_1.6" } ) }}

Revision Source

<p>
</p><p>JavaScript 1.6 introduces several new features: E4X, several new <code>Array</code> methods, and Array and String generics.
</p><p>JavaScript 1.6 is supported in <a href="en/Firefox_1.5">Firefox 1.5</a> and later.
</p>
<h3 name="E4X"> E4X </h3>
<p>ECMAScript for XML (<a href="en/E4X">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>You can still use the standard MIME type when using E4X:
</p>
<pre>&lt;script type="text/javascript"&gt;
</pre>
<p>However, E4X syntax may conflict with the common practice of putting scripts into HTML comments (<code><span class="plain">&lt;!--...--&gt;</span></code>) to hide them from old browsers. E4X may also conflict with the more modern practice of putting scripts within XML CDATA sections (<code>&lt;![CDATA{{mediawiki.external('...')}}]&gt;</code>) to allow the symbols "&lt;" and "&gt;" in the script (note that this does not apply to HTML). If you see inexplicable syntax errors, add "; e4x=1" to the MIME type:
</p>
<pre>&lt;script type="text/javascript; e4x=1"&gt;
</pre>
<p>Note that scripts in extensions always treat HTML comments as E4X literals. That is, the "e4x=1" is implicit.
</p><p>E4X is documented in <a href="en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X">Processing XML with E4X</a>.
</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 items in the array while that function is returning true. It returns true if the function returns true for every item it could visit.
</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 items in the array while that function returns false. It returns true if the function returns true for any item it could visit.
</li></ul>
<p>For more information see <a href="en/Core_JavaScript_1.5_Guide/Working_with_Arrays#Introduced_in_JavaScript_1.6">Working with Arrays</a>, or consult Nicholas C. Zakas' article, <a class="external" href="http://www.webreference.com/programming/javascript/ncz/column4/index.html">Mozilla's New Array Methods</a>.
</p>
<h3 name="Array_and_String_generics"> Array and String generics </h3>
<p>Sometimes you would like to apply array methods to strings.  By doing this, you treat a string as an array of characters. For example, 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>
<p>See also <a href="en/Core_JavaScript_1.5_Guide/Working_with_Arrays#Working_with_Array-like_objects">Working with Array-like objects</a>.
</p>
<div class="noinclude">
</div>
{{ wiki.languages( { "ru": "ru/\u041d\u043e\u0432\u043e\u0435_\u0432_JavaScript_1.6", "es": "es/Novedades_en_JavaScript_1.6", "fr": "fr/Nouveaut\u00e9s_dans_JavaScript_1.6", "ja": "ja/New_in_JavaScript_1.6", "pl": "pl/Nowo\u015bci_w_JavaScript_1.6", "zh-cn": "cn/New_in_JavaScript_1.6" } ) }}
Revert to this revision