mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2026-04-27 05:44:56 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -4,10 +4,10 @@
|
||||
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/functions-restparameters.html" width="100%"></iframe></div>
|
||||
<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre class="brush: js">function(a, b, ...theArgs) {
|
||||
<pre><code class="language-javascript">function(a, b, ...theArgs) {
|
||||
// ...
|
||||
}
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="描述">描述</h2>
|
||||
<p>如果函数的最后一个命名参数以<code>...</code>为前缀,则它将成为一个由剩余参数组成的真数组,其中从<code>0</code>(包括)到<code>theArgs.length</code>(排除)的元素由传递给函数的实际参数提供。</p>
|
||||
<p>在上面的例子中,<code>theArgs</code>将收集该函数的第三个参数(因为第一个参数被映射到<code>a</code>,而第二个参数映射到<code>b</code>)和所有后续参数。</p>
|
||||
@@ -40,27 +40,27 @@ function f(a, b) {
|
||||
function f(...args) {
|
||||
var normalArray = args;
|
||||
var first = normalArray.shift(); // OK, gives the first argument
|
||||
}</code></pre>
|
||||
}</code></code></pre>
|
||||
<h3 id="解构剩余参数">解构剩余参数</h3>
|
||||
<p>剩余参数可以被解构,这意味着他们的数据可以被解包到不同的变量中。请参阅<a href="Reference/Operators/Destructuring_assignment">解构赋值</a>。</p>
|
||||
<pre class="brush: js">function f(...[a, b, c]) {
|
||||
<pre><code class="language-javascript">function f(...[a, b, c]) {
|
||||
return a + b + c;
|
||||
}
|
||||
|
||||
f(1) // NaN (b and c are undefined)
|
||||
f(1, 2, 3) // 6
|
||||
f(1, 2, 3, 4) // 6 (the fourth parameter is not destructured)</pre>
|
||||
f(1, 2, 3, 4) // 6 (the fourth parameter is not destructured)</code></pre>
|
||||
<h2 id="Example" name="Example">示例</h2>
|
||||
<p>因为<code>theArgs</code>是个数组,所以你可以使用<code>length</code>属性得到剩余参数的个数:</p>
|
||||
<pre class="brush: js">function fun1(...theArgs) {
|
||||
<pre><code class="language-javascript">function fun1(...theArgs) {
|
||||
alert(theArgs.length);
|
||||
}
|
||||
|
||||
fun1(); // 弹出 "0", 因为theArgs没有元素
|
||||
fun1(5); // 弹出 "1", 因为theArgs只有一个元素
|
||||
fun1(5, 6, 7); // 弹出 "3", 因为theArgs有三个元素</pre>
|
||||
fun1(5, 6, 7); // 弹出 "3", 因为theArgs有三个元素</code></pre>
|
||||
<p>下例中,剩余参数包含了从第二个到最后的所有实参,然后用第一个实参依次乘以它们:</p>
|
||||
<pre class="brush: js">function multiply(multiplier, ...theArgs) {
|
||||
<pre><code class="language-javascript">function multiply(multiplier, ...theArgs) {
|
||||
return theArgs.map(function (element) {
|
||||
return multiplier * element;
|
||||
});
|
||||
@@ -68,9 +68,9 @@ fun1(5, 6, 7); // 弹出 "3", 因为theArgs有三个元素</pre>
|
||||
|
||||
var arr = multiply(2, 1, 2, 3);
|
||||
console.log(arr); // [2, 4, 6]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>下例演示了你可以在剩余参数上使用任意的数组方法,而<code>arguments</code>对象不可以:</p>
|
||||
<pre class="brush: js">function sortRestArgs(...theArgs) {
|
||||
<pre><code class="language-javascript">function sortRestArgs(...theArgs) {
|
||||
var sortedArgs = theArgs.sort();
|
||||
return sortedArgs;
|
||||
}
|
||||
@@ -83,14 +83,14 @@ function sortArguments() {
|
||||
}
|
||||
|
||||
alert(sortArguments(5,3,7,1)); // 抛出TypeError异常:arguments.sort is not a function
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>为了在<code>arguments</code>对象上使用<code>Array</code>方法,它必须首先被转换为一个真正的数组。</p>
|
||||
<pre class="brush: js">function sortArguments() {
|
||||
<pre><code class="language-javascript">function sortArguments() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var sortedArgs = args.sort();
|
||||
return sortedArgs;
|
||||
}
|
||||
console.log(sortArguments(5, 3, 7, 1)); // shows 1, 3, 5, 7</pre>
|
||||
console.log(sortArguments(5, 3, 7, 1)); // shows 1, 3, 5, 7</code></pre>
|
||||
<h2 id="Specifications" name="Specifications">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user