diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/pop/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/array/pop/index.html | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/pop/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/pop/index.html new file mode 100644 index 0000000000..51fc625019 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/array/pop/index.html @@ -0,0 +1,101 @@ +--- +title: Array.prototype.pop() +slug: Web/JavaScript/Reference/Global_Objects/Array/pop +tags: + - Array + - Array.prototype.pop() + - ES5 + - ES6 + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Array/pop +--- +<p>{{JSRef}}</p> + +<p><code><strong>pop()</strong></code>方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。</p> + +<div>{{EmbedInteractiveExample("pages/js/array-pop.html")}}</div> + + + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><em>arr</em>.pop()</code></pre> + +<h3 id="返回值">返回值</h3> + +<p>从数组中删除的元素(当数组为空时返回{{jsxref("undefined")}})。</p> + +<h2 id="Description" name="Description">描述</h2> + +<p><code>pop</code> 方法从一个数组中删除并返回最后一个元素。</p> + +<p><code>pop</code> 方法有意具有通用性。该方法和 {{jsxref("Function.call", "call()")}} 或 {{jsxref("Function.apply", "apply()")}} 一起使用时,可应用在类似数组的对象上。<code>pop</code>方法根据 <code>length</code>属性来确定最后一个元素的位置。如果不包含<code>length</code>属性或<code>length</code>属性不能被转成一个数值,会将<code>length</code>置为0,并返回<code>undefined</code>。</p> + +<p>如果你在一个空数组上调用 pop(),它返回 {{jsxref("undefined")}}。</p> + +<h2 id="Example" name="Example">示例</h2> + +<h3 id="Example:_Removing_the_last_element_of_an_array" name="Example:_Removing_the_last_element_of_an_array">例子: 删除掉数组的最后一个元素</h3> + +<p>下面的代码首先创建了一个拥有四个元素的数组 myFish,然后删除掉它的最后一个元素。</p> + +<pre class="brush:js">let myFish = ["angel", "clown", "mandarin", "surgeon"]; + +let popped = myFish.pop(); + +console.log(myFish); +// ["angel", "clown", "mandarin"] + +console.log(popped); +// surgeon +</pre> + +<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2> + +<table> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Initial definition. Implemented in JavaScript 1.2.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.4.4.6', 'Array.prototype.pop')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-array.prototype.pop', 'Array.prototype.pop')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-array.prototype.pop', 'Array.prototype.pop')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("javascript.builtins.Array.pop")}}</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Array.prototype.push()")}}</li> + <li>{{jsxref("Array.prototype.shift()")}}</li> + <li>{{jsxref("Array.prototype.unshift()")}}</li> + <li>{{jsxref("Array.prototype.concat()")}}</li> + <li>{{jsxref("Array.prototype.splice()")}}</li> +</ul> |