MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

JavaScript 1.8 是 Gecko 1.9(包含於 Firefox 3)的一部分。相較於 JavaScript 1.7 此版只是個小更新,不過部分更新是為了跟進 ECMAScript 4/JavaScript 2。此次的釋出版本包含所有在 JavaScript 1.6JavaScript 1.7 出現的新機能。

詳見 bug 380236 以追蹤 JavaScript 1.8 的開發狀態。本文件的狀態可見 bug 421027

使用 JavaScript 1.8

為了在 HTML 中使用 JavaScript 1.8 的新機能,需要像下面那樣來使用︰

 <script type="application/javascript;version=1.8"> ... 你的代碼 ... </script>

另一個方式(不推薦)是使用已廢棄的 <script> 語言屬性並把他定義為 "JavaScript1.8"。

如果是使用 JavaScript shell,JavaScript XPCOM 元件,或 XUL <script> 元件,會自動(bug 381031, bug 385159)使用最新的 JS 版本(在 Mozilla 1.9 中是 JS1.8)。

如果某一機能需要使用到新關鍵字 "yield" 和 "let",你就要指定 1.7 以上的版本,因為現存的代碼可能會使用這些關鍵字作為變數或函數名。至於未使用到新關鍵字的機能(例如產生器表達式)可以直接使用,不需指定 JavaScript 版本。

表達式化簡

表達式化簡(Expression closures)提供類似 典型 Lambda 記法 的語法,除了用於簡單函數的簡寫以外,並未帶來其他新機能。

JavaScript 1.7 及早期版本︰

 function(x) { return x * x; }

JavaScript 1.8︰

 function(x) x * x

這個語法可讓你省略花括弧和 'return' 語法 - 使這些隱含化。以這種風格編寫的代碼除了簡短以外,並沒有其他額外的優點。

範例:

連結事件接收器的簡寫︰

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

在來自 JavaScript 1.6 的 some 中使用這個記法︰

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

產生器表達式

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.

陣列更進一步的擴充

There are two new iterative Array methods included in JavaScript 1.8, specifically:

  • 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.

for..in 分割代入的變更

One change that occurred in the release of JavaScript 1.8 was a bug fix related to the key/value destructuring of arrays introduced in JavaScript 1.7. Previously it was possible to destructure the keys/values of an array by using for ( var [key, value] in array ). However that made it impossible to destructure the values of an array - that were arrays. This has been resolved now. (bug 366941).

文件標籤與貢獻者

 此頁面的貢獻者: jackblackevo, fscholz, irvinfly, happysadman
 最近更新: jackblackevo,