mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-16 15:54:22 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -6,37 +6,37 @@
|
||||
<h2 id="字面值中的尾后逗号">字面值中的尾后逗号</h2>
|
||||
<h3 id="数组">数组</h3>
|
||||
<p>JavaScript 忽略数组中的尾后逗号:</p>
|
||||
<pre class="brush: js">var arr = [
|
||||
<pre><code class="language-javascript">var arr = [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
];
|
||||
|
||||
arr; // [1, 2, 3]
|
||||
arr.length; // 3</pre>
|
||||
arr.length; // 3</code></pre>
|
||||
<p>如果使用了多于一个尾后逗号,会产生省略(或者间隙)。 带有间隙的数组叫做<em>稀疏</em>数组(<em>密致</em>数组没有间隙)。 例如,当使用 <a href="Reference/Global_Objects/Array/forEach" title="forEach() 方法对数组的每个元素执行一次提供的函数。"><code>Array.prototype.forEach()</code></a> 或 <a href="Reference/Global_Objects/Array/map" title="map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。"><code>Array.prototype.map()</code></a> 迭代数组时,会跳过数组间隙。</p>
|
||||
<pre class="brush: js">var arr = [1, 2, 3,,,];
|
||||
<pre><code class="language-javascript">var arr = [1, 2, 3,,,];
|
||||
arr.length; // 5
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="对象">对象</h3>
|
||||
<p>从 ECMAScript 5 开始,对象字面值中的尾后逗号也是合法的:</p>
|
||||
<pre class="brush: js">var object = {
|
||||
<pre><code class="language-javascript">var object = {
|
||||
foo: "bar",
|
||||
baz: "qwerty",
|
||||
age: 42,
|
||||
};</pre>
|
||||
};</code></pre>
|
||||
<h2 id="函数中的尾后逗号">函数中的尾后逗号</h2>
|
||||
<p>ECMAScript 2017 支持函数参数中的尾后逗号。</p>
|
||||
<h3 id="参数定义">参数定义</h3>
|
||||
<p>下面的两个函数定义都是合法的,并且互相等价。尾后逗号并不影响函数定义,或者其<code>arguments</code>对象的 <code>length</code>属性。</p>
|
||||
<pre class="brush: js">function f(p) {}
|
||||
<pre><code class="language-javascript">function f(p) {}
|
||||
function f(p,) {}
|
||||
|
||||
(p) => {};
|
||||
(p,) => {};
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>尾后逗号也可用于类或对象的<a href="/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions">方法定义</a>。</p>
|
||||
<pre class="brush: js">class C {
|
||||
<pre><code class="language-javascript">class C {
|
||||
one(a,) {},
|
||||
two(a, b,) {},
|
||||
}
|
||||
@@ -45,27 +45,27 @@ var obj = {
|
||||
one(a,) {},
|
||||
two(a, b,) {},
|
||||
};
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="函数调用">函数调用</h3>
|
||||
<p>下面的两个函数调用都是合法的,并且互相等价。</p>
|
||||
<pre class="brush: js">f(p);
|
||||
<pre><code class="language-javascript">f(p);
|
||||
f(p,);
|
||||
|
||||
Math.max(10, 20);
|
||||
Math.max(10, 20,);
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="不合法的尾后逗号">不合法的尾后逗号</h3>
|
||||
<p>仅仅包含逗号的函数参数定义或者函数调用会抛出 <a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a>。 而且,当使用<a href="/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">剩余参数</a>的时候,并不支持尾后逗号:</p>
|
||||
<pre class="brush: js example-bad">function f(,) {} // SyntaxError: missing formal parameter
|
||||
<pre><code class="language-js example-bad">function f(,) {} // SyntaxError: missing formal parameter
|
||||
(,) => {}; // SyntaxError: expected expression, got ','
|
||||
f(,) // SyntaxError: expected expression, got ','
|
||||
|
||||
function f(...p,) {} // SyntaxError: parameter after rest parameter
|
||||
(...p,) => {} // SyntaxError: expected closing parenthesis, got ','
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="解构中的尾后逗号">解构中的尾后逗号</h2>
|
||||
<p>在使用<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">解构赋值</a>时,尾后逗号也可以用于左侧:</p>
|
||||
<pre class="brush: js">// 带有尾后逗号的数组解构
|
||||
<pre><code class="language-javascript">// 带有尾后逗号的数组解构
|
||||
[a, b,] = [1, 2];
|
||||
|
||||
// 带有尾后逗号的对象解构
|
||||
@@ -74,21 +74,21 @@ var o = {
|
||||
q: true,
|
||||
};
|
||||
var {p, q,} = o;
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>同样,使用剩余参数时,出抛出 <a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a>:</p>
|
||||
<pre class="brush: js example-bad">var [a, ...b,] = [1, 2, 3];
|
||||
// SyntaxError: rest element may not have a trailing comma</pre>
|
||||
<pre><code class="language-js example-bad">var [a, ...b,] = [1, 2, 3];
|
||||
// SyntaxError: rest element may not have a trailing comma</code></pre>
|
||||
<h2 id="JSON_中的尾后逗号">JSON 中的尾后逗号</h2>
|
||||
<p>对象中的尾后逗号仅仅在 ECMAScript 5 中引入。由于 JSON 基于 ES5 之前的语法, <strong>JSON 中并不支持尾后逗号</strong>。</p>
|
||||
<p>下面两行都会抛出 <code>SyntaxError</code>:</p>
|
||||
<pre class="brush: js example-bad">JSON.parse('[1, 2, 3, 4, ]');
|
||||
<pre><code class="language-js example-bad">JSON.parse('[1, 2, 3, 4, ]');
|
||||
JSON.parse('{"foo" : 1, }');
|
||||
// SyntaxError JSON.parse: unexpected character
|
||||
// at line 1 column 14 of the JSON data
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>去掉尾后逗号,使 JSON 正确解析:</p>
|
||||
<pre class="brush: js example-good">JSON.parse('[1, 2, 3, 4 ]');
|
||||
JSON.parse('{"foo" : 1 }');</pre>
|
||||
<pre><code class="language-js example-good">JSON.parse('[1, 2, 3, 4 ]');
|
||||
JSON.parse('{"foo" : 1 }');</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user