Visit Mozilla.org

Nowości w JavaScript 1.8

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

Ten artykuł obejmuje funkcje wprowadzone w Firefoksie 3

Obsługa języka JavaScript w wersji 1.8 ma zostać wprowadzona wraz z wydaniem silnika Gecko 1.9 (który ma się znaleźć w programie Firefox 3). Jest to aktualizacja mniej znacząca niż JavaScript 1.7; zmiany odpowiadają modyfikacjom dokonanym ostatnio w języku ECMAScript 4/JavaScript 2. W tym wydaniu zawarte będą wszystkie nowe funkcje wprowadzone w wydaniach JavaScript 1.6 i JavaScript 1.7.

Aby zapoznać się z postępem prac nad rozwojem obsługi języka JavaScript w wersji 1.8, patrz błąd 380236.

Spis treści

[edytuj] Korzystanie z języka JavaScript 1.8

Aby skorzystać z nowych własności języka JavaScript 1.8 w kodzie HTML, należy użyć następującego kodu:

 <script type="application/javascript;version=1.8"> ... twój kod ... </script>

W przypadku korzystania z powłoki JavaScript, komponentów XPCOM języka JavaScript lub elementów <script> w języku XUL automatycznie używana jest najnowsza wersja JS (JS1.8 w Mozilli 1.9); patrz błąd 381031 i błąd 385159.

W przypadku korzystania z funkcji, w których wymagane jest użycie nowych słów kluczowych yield i let, należy określić, że używana ma być wersja języka 1.7 lub nowsza — w istniejącym kodzie te słowa kluczowe mogą być bowiem nazwami zmiennych lub funkcji. Funkcje, w których nie wprowadzono nowych słów kluczowych (takie jak wyrażenia generatorów), mogą być używane bez określania wersji języka JavaScript.

[edytuj] Zakończenia wyrażeń

This addition is nothing more than a shorthand for writing simple functions, giving the language something similar to a typical Lambda notation.

JavaScript 1.7 and older:

 function(x) { return x * x; }

JavaScript 1.8:

 function(x) x * x

This syntax allows you to leave off the braces and 'return' statement - making them implicit. There is no added benefit to writing code in this manner, other than having it be syntactically shorter.

Przykłady:

A shorthand for binding event listeners:

 document.addEventListener("click", function() false, true);

Using this notation with some of the array functions from JavaScript 1.6:

 elems.some(function(elem) elem.type == "text");

[edytuj] Wyrażenia generatorów

This addition allows you to simply create generators (which were introduced in JavaScript 1.7). Typically you would have to create a custom function which would have a yield in it, but this addition allows you to use array comprehension-like syntax to create an identical generator statement.

In JavaScript 1.7, you might write something like the following in order to create a custom generator for an object:

 function add3(obj) {
   for ( let i in obj )
     yield i + 3;
 }
 
 let it = add3(someObj);
 try {
   while (true) {
     document.write(it.next() + "<br>\n");
   }
 } catch (err if err instanceof StopIteration) {
   document.write("End of record.<br>\n");
 }

In JavaScript 1.8, you can circumvent having to create a custom generator function by using a generator expression instead:

 let it = (i + 3 for (i in someObj));
 try {
   while (true) {
     document.write(it.next() + "<br>\n");
   }
 } catch (err if err instanceof StopIteration) {
   document.write("End of record.<br>\n");
 }

Generator expressions can also be passed in, as values, to a function. This is particularly noteworthy since generators aren't run until they are absolutely needed (unlike for typical array comprehension situations, where the arrays are constructed ahead of time). An example of the difference can be seen here:

Using JavaScript 1.7 Array Comprehension

 handleResults([ i for ( i in obj ) if ( i > 3 ) ]);
 
 function handleResults( results ) {
   for ( let i in results )
     // ...
 }

Using JavaScript 1.8 Generator Expressions

 handleResults( i for ( i in obj ) if ( i > 3 ) );
 
 function handleResults( results ) {
   for ( let i in results )
     // ...
 }

The significant difference between the two examples being that by using the generator expressions, you would only have to loop over the 'obj' structure once, total, as opposed to once when comprehending the array, and again when iterating through it.

[edytuj] Dodatkowe zmiany w obiekcie Array

W jezyku JavaScript 1.8 dostępne są dwie nowe metody iteracyjne obiektu Array:

  • reduce() - runs a function on every item in the array and collects the results from previous calls.
  • reduceRight() - runs a function on every item in the array and collects the results from previous calls, but in reverse.


[edytuj] Changes in destructuring for..in

TBD: mention Nowości w JavaScript 1.7#Iterowanie przez obiekty (błąd 366941).

[edytuj] Upcoming changes

Changes expected to arrive in JavaScript 1.8 include: