2019-04-21 11:50:48 +08:00

21 lines
3.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<p>以下为JavaScript 1.8的更新日志。JavaScript 1.8 是 Gecko 1.9(已合并在 <a href="/cn/Firefox_3" title="cn/Firefox_3">Firefox 3</a> 中)的一部分。参见 <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=380236" rel="noopener" title="FIXED: JS1.8 tracking bug">bug 380236</a> 以跟踪 JavaScript 1.8。</p>
<h3 id=".E4.BD.BF.E7.94.A8_JavaScript_1.8" name=".E4.BD.BF.E7.94.A8_JavaScript_1.8">使用 JavaScript 1.8</h3>
<p>为了可以在 HTML 中使用 JavaScript 1.8 的新特性,需要这样写:</p>
<pre class="eval"> &lt;script type="application/javascript;version=1.8"&gt; ... 你的代码 ... &lt;/script&gt;
</code></pre>
<p>另一种方法(不推荐)是使用旧式的 &lt;script&gt; 属性 language把它定义为 "JavaScript1.8"。</p>
<p>在使用 <a href="https://developer.mozilla.org/zh-CN/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell" title="Cn/Introduction_to_the_JavaScript_shell">JavaScript shell</a> 、JavaScript XPCOM 组件,或者 XUL <code>&lt;script&gt;</code> 元素的时候将自动启用最新的JS版本Mozilla 1.9中的 JS1.8(<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=381031" rel="noopener" title="FIXED: make JS1.8 the default for &lt;xul:script&gt;">bug 381031</a>, <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=385159" rel="noopener" title="FIXED: Make the JS shell use the latest version">bug 385159</a>)。</p>
<p>如果需要使用新的关键字 "yield" 和 "let"你需要指定是1.7版本或者更高的版本,因为现有的代码可能会把这两个关键字用作变量名或者函数名。如果要使用的新特性没有引入任何新的关键字(例如生成器表达式),就可以不指定 JavaScript 的版本。</p>
<h2 id="JavaScript_1.8新特性">JavaScript 1.8新特性</h2>
<ul>
<li><a href="Reference/Operators/Expression_Closures" title="表达式闭包是定义简单函数的一种便捷方式。">Expression Closures</a>.</li>
<li><a href="Guide/Iterators_and_Generators">生成器表达式</a>。生成器表达式可以让你方便地创建生成器在JavaScript1.7中引入。通常你需要创建一个内含yield的自定义函数来得到一个生成器而生成器表达式可以让你使用类似数组的语法来达到同样的目的。</li>
<li><a href="https://developer.mozilla.orgReference/Global_Objects/Array/Reduce"><code>Array.prototype.reduce()</code></a></li>
<li><a href="https://developer.mozilla.orgReference/Global_Objects/Array/ReduceRight"><code>Array.prototype.reduceRight()</code></a></li>
</ul>
<h2 id="JavaScript_1.8功能更新">JavaScript 1.8功能更新</h2>
<h3 id="对for..in解构的修改"><code>对for..in解构的修改</code></h3>
<p>JavaScript1.8中的一个修改是对JavaScript1.7中引入的数组键值结构相关的bug修复。之前可以用<code>for ( var [key, value] in array )的方式来结构一个数组的键值。但是这也让对数组的数组的键值结构变得不可能比如一个迭代器返回一个当前键值对的数组。现在这个问题可以用for ( var [key, value] in Iterator(array))来解决</code>(<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=366941" rel="noopener" title='FIXED: Get rid of the "for([key, value] in obj)" form so that normal array destructuring works in for..in'>bug 366941</a>)。</p>
</article>