aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/array/pop/index.html
blob: 51fc625019f39b996dedbbc7e4fa959efd92305f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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>