mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-16 07:51:52 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -5,33 +5,33 @@
|
||||
<p class="hidden">该示例的源代码存放于Github中,如果你想进行修订,请先克隆<a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a>, 修改完成之后再通过pull request的方式推送给源仓库。</p>
|
||||
<h2 id="语法"><font><font>语法</font></font></h2>
|
||||
<p><font><font>函数调用:</font></font></p>
|
||||
<pre class="syntaxbox">myFunction(...iterableObj);</pre>
|
||||
<pre><code class="language-javascript">myFunction(...iterableObj);</code></pre>
|
||||
<p><font><font>字面量数组构造或字符串:</font></font></p>
|
||||
<pre>[...iterableObj, '4', ...'hello', 6];</pre>
|
||||
<pre>[...iterableObj, '4', ...'hello', 6];</code></pre>
|
||||
<p>构造字面量对象时,进行克隆或者属性拷贝(ECMAScript 2018规范新增特性):</p>
|
||||
<pre class="syntaxbox">let objClone = { ...obj };</pre>
|
||||
<pre><code class="language-javascript">let objClone = { ...obj };</code></pre>
|
||||
<h2 id="示例"><font><font>示例</font></font></h2>
|
||||
<h3 id="在函数调用时使用展开语法">在函数调用时使用展开语法</h3>
|
||||
<h4 id="等价于apply的方式">等价于apply的方式</h4>
|
||||
<p><font><font>如果想将数组元素迭代为函数参数,一般使用</font></font><a href="Reference/Global_Objects/Function/apply" title="apply() 方法调用一个具有给定this值的函数,以及作为一个数组(或类似数组对象)提供的参数。"><code>Function.prototype.apply</code></a> 的方式进行调用<font><font>。</font></font></p>
|
||||
<pre class="brush: js">function myFunction(x, y, z) { }
|
||||
<pre><code class="language-javascript">function myFunction(x, y, z) { }
|
||||
var args = [0, 1, 2];
|
||||
myFunction.apply(null, args);</pre>
|
||||
myFunction.apply(null, args);</code></pre>
|
||||
<p><font><font>有了展开语法,可以这样写:</font></font></p>
|
||||
<pre class="brush: js"><code>function myFunction(x, y, z) { }
|
||||
<pre><code class="language-javascript"><code>function myFunction(x, y, z) { }
|
||||
var args = [0, 1, 2];
|
||||
myFunction(...args);</code></pre>
|
||||
myFunction(...args);</code></code></pre>
|
||||
<p><font><font>所有参数都可以通过展开语法来传值,也不限制多次使用展开语法。</font></font></p>
|
||||
<pre class="brush: js">function myFunction(v, w, x, y, z) { }
|
||||
<pre><code class="language-javascript">function myFunction(v, w, x, y, z) { }
|
||||
var args = [0, 1];
|
||||
myFunction(-1, ...args, 2, ...[3]);</pre>
|
||||
myFunction(-1, ...args, 2, ...[3]);</code></pre>
|
||||
<h4 id="在_new_表达式中应用">在 new 表达式中应用</h4>
|
||||
<p><font><font>使用 </font></font><code>new</code><font><font> 关键字来调用构造函数时</font></font><font><font>,不能</font></font><strong><font><font>直接</font></font></strong><font><font>使用数组+ </font></font><code>apply</code><font><font> 的方式(</font></font><code>apply</code><font><font> 执行的是调用 </font></font><code>[[Call]]</code><font><font> , 而不是构造 </font></font><code>[[Construct]]</code><font><font>)。当然</font><font>, 有了展开语法, 将数组展开为构造函数的参数就很简单了:</font></font></p>
|
||||
<pre class="brush: js"><font><font>var dateFields = [1970, 0, 1]; </font><font>// 1970年1月1日</font></font><font><font>
|
||||
<pre><code class="language-javascript"><font><font>var dateFields = [1970, 0, 1]; </font><font>// 1970年1月1日</font></font><font><font>
|
||||
var d = new Date(...dateFields);</font></font>
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p><font><font>如果不使用展开语法, 想将数组元素传给构造函数, 实现方式可能是这样的</font></font><font><font>:</font></font></p>
|
||||
<pre class="brush: js">function applyAndNew(constructor, args) {
|
||||
<pre><code class="language-javascript">function applyAndNew(constructor, args) {
|
||||
function partial () {
|
||||
return constructor.apply(this, args);
|
||||
};
|
||||
@@ -55,54 +55,54 @@ var myConstructorWithArguments = applyAndNew(myConstructor, myArguments);
|
||||
console.log(new myConstructorWithArguments);
|
||||
// (myConstructor构造函数中): arguments.length: 6
|
||||
// (myConstructor构造函数中): ["hi", "how", "are", "you", "mr", null]
|
||||
// ("new myConstructorWithArguments"中): {prop1: "val1", prop2: "val2"}</pre>
|
||||
// ("new myConstructorWithArguments"中): {prop1: "val1", prop2: "val2"}</code></pre>
|
||||
<h3 id="构造字面量数组时使用展开语法"><font><font>构造字面量数组时使用展开语法</font></font></h3>
|
||||
<h4 id="构造字面量数组时更给力!"><font><font>构造字面量数组时更给力!</font></font></h4>
|
||||
<p><font><font>没有展开语法的时候,只能组合使用 </font></font><code>push</code><font><font>, </font></font><code>splice</code><font><font>, </font></font><code>concat</code><font><font> 等方法,来将已有数组元素变成新数组的一部分。有了展开语法, 通过字面量方式, 构造新数组会变得更简单、更优雅:</font></font></p>
|
||||
<pre class="brush: js"><font><font>var parts = ['shoulders', 'knees'];
|
||||
<pre><code class="language-javascript"><font><font>var parts = ['shoulders', 'knees'];
|
||||
var lyrics = ['head', ...parts, 'and', 'toes']; </font></font>
|
||||
// ["head", "shoulders", "knees", "and", "toes"]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>和参数列表的展开类似, <code>...</code> 在构造字面量数组时, 可以在任意位置多次使用.</p>
|
||||
<h4 id="数组拷贝(copy)">数组拷贝(copy)</h4>
|
||||
<pre class="brush: js">var arr = [1, 2, 3];
|
||||
<pre><code class="language-javascript">var arr = [1, 2, 3];
|
||||
var arr2 = [...arr]; // like arr.slice()
|
||||
arr2.push(4);
|
||||
|
||||
// arr2 此时变成 [1, 2, 3, 4]
|
||||
// arr 不受影响
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p><strong>提示:</strong> 实际上, 展开语法和 <a href="Reference/Global_Objects/Object/assign" title="Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。"><code>Object.assign()</code></a> 行为一致, 执行的都是浅拷贝(只遍历一层)。如果想对多维数组进行深拷贝, 下面的示例就有些问题了。</p>
|
||||
<pre class="brush: js">var a = [[1], [2], [3]];
|
||||
<pre><code class="language-javascript">var a = [[1], [2], [3]];
|
||||
var b = [...a];
|
||||
b.shift().shift(); // 1
|
||||
// Now array a is affected as well: [[], [2], [3]]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h4 id="连接多个数组">连接多个数组</h4>
|
||||
<p><a href="Reference/Global_Objects/Array/concat" title="concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。"><code>Array.concat</code></a> 函数常用于将一个数组连接到另一个数组的后面。如果不使用展开语法, 代码可能是下面这样的:</p>
|
||||
<pre class="brush: js">var arr1 = [0, 1, 2];
|
||||
<pre><code class="language-javascript">var arr1 = [0, 1, 2];
|
||||
var arr2 = [3, 4, 5];
|
||||
// 将 arr2 中所有元素附加到 arr1 后面并返回
|
||||
var arr3 = arr1.concat(arr2);</pre>
|
||||
var arr3 = arr1.concat(arr2);</code></pre>
|
||||
<p>使用展开语法:</p>
|
||||
<pre class="brush: js">var arr1 = [0, 1, 2];
|
||||
<pre><code class="language-javascript">var arr1 = [0, 1, 2];
|
||||
var arr2 = [3, 4, 5];
|
||||
var arr3 = [...arr1, ...arr2];
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p><a href="Reference/Global_Objects/Array/unshift" title="unshift() 方法将一个或多个元素添加到数组的开头,并返回该数组的新长度。"><code>Array.unshift</code></a> 方法常用于在数组的开头插入新元素/数组. 不使用展开语法, 示例如下:</p>
|
||||
<pre class="brush: js">var arr1 = [0, 1, 2];
|
||||
<pre><code class="language-javascript">var arr1 = [0, 1, 2];
|
||||
var arr2 = [3, 4, 5];
|
||||
// 将 arr2 中的元素插入到 arr1 的开头
|
||||
Array.prototype.unshift.apply(arr1, arr2) // arr1 现在是 [3, 4, 5, 0, 1, 2]</pre>
|
||||
Array.prototype.unshift.apply(arr1, arr2) // arr1 现在是 [3, 4, 5, 0, 1, 2]</code></pre>
|
||||
<p>如果使用展开语法, 代码如下: [请注意, 这里使用展开语法创建了一个新的 <code>arr1</code> 数组, <a href="Reference/Global_Objects/Array/unshift" title="unshift() 方法将一个或多个元素添加到数组的开头,并返回该数组的新长度。"><code>Array.unshift</code></a> 方法则是修改了原本存在的 <code>arr1</code> 数组]:</p>
|
||||
<pre class="brush: js">var arr1 = [0, 1, 2];
|
||||
<pre><code class="language-javascript">var arr1 = [0, 1, 2];
|
||||
var arr2 = [3, 4, 5];
|
||||
arr1 = [...arr2, ...arr1]; // arr1 现在为 [3, 4, 5, 0, 1, 2]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="构造字面量对象时使用展开语法">构造<font><font>字面量对象时使用展开语法</font></font></h3>
|
||||
<p><a class="external" href="https://github.com/tc39/proposal-object-rest-spread" rel="noopener">Rest/Spread Properties for ECMAScript</a> 提议(stage 4) 对 <a href="Reference/Operators/Object_initializer">字面量对象</a> 增加了展开特性。其行为是, 将已有对象的所有可枚举(enumerable)属性拷贝到新构造的对象中.</p>
|
||||
<p>浅拷贝(Shallow-cloning, 不包含 prototype) 和对象合并, 可以使用更简短的展开语法。而不必再使用 <a href="Reference/Global_Objects/Object/assign" title="Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。"><code>Object.assign()</code></a> 方式.</p>
|
||||
<pre class="brush: js">var obj1 = { foo: 'bar', x: 42 };
|
||||
<pre><code class="language-javascript">var obj1 = { foo: 'bar', x: 42 };
|
||||
var obj2 = { foo: 'baz', y: 13 };
|
||||
|
||||
var clonedObj = { ...obj1 };
|
||||
@@ -110,7 +110,7 @@ var clonedObj = { ...obj1 };
|
||||
|
||||
var mergedObj = { ...obj1, ...obj2 };
|
||||
// 合并后的对象: { foo: "baz", x: 42, y: 13 }
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p><strong>提示</strong>: <a href="Reference/Global_Objects/Object/assign" title="Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。"><code>Object.assign()</code></a> 函数会触发 <a href="Reference/Functions/set">setters</a>,而展开语法则不会。</p>
|
||||
<p> </p>
|
||||
<p><strong>提示</strong>: 不能替换或者模拟 <a href="Reference/Global_Objects/Object/assign" title="Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。"><code>Object.assign()</code></a> 函数:</p>
|
||||
@@ -122,14 +122,14 @@ var mergedObj = merge ( obj1, obj2);
|
||||
// Object { 0: { foo: 'bar', x: 42 }, 1: { foo: 'baz', y: 13 } }
|
||||
|
||||
var mergedObj = merge ( {}, obj1, obj2);
|
||||
// Object { 0: {}, 1: { foo: 'bar', x: 42 }, 2: { foo: 'baz', y: 13 } }</code></pre>
|
||||
// Object { 0: {}, 1: { foo: 'bar', x: 42 }, 2: { foo: 'baz', y: 13 } }</code></code></pre>
|
||||
<p>在这段代码中, 展开操作符(spread operator)并没有按预期的方式执行: 而是先将多个解构变为剩余参数(rest parameter), 然后再将剩余参数展开为字面量对象.</p>
|
||||
<p> </p>
|
||||
<h3 id="只能用于可迭代对象">只能用于可迭代对象</h3>
|
||||
<p>在数组或函数参数中使用展开语法时, 该语法只能用于 <a href="Reference/Global_Objects/Symbol/iterator">可迭代对象</a>:</p>
|
||||
<pre class="brush: js">var obj = {'key1': 'value1'};
|
||||
<pre><code class="language-javascript">var obj = {'key1': 'value1'};
|
||||
var array = [...obj]; // TypeError: obj is not iterable
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="展开多个值">展开多个值</h3>
|
||||
<p>在函数调用时使用展开语法,请注意不能超过 JavaScript 引擎限制的最大参数个数。更多详细信息,请参考: <a href="Reference/Global_Objects/Function/apply" title="apply()方法使用给定的这个值调用一个函数,并以一个数组(或类似数组的对象)的形式提供参数。"><code>apply()</code></a>。</p>
|
||||
<h2 id="剩余语法(剩余参数)">剩余语法(剩余参数)</h2>
|
||||
|
||||
Reference in New Issue
Block a user