aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/array/reverse/index.html
blob: b408aa046f5f37cc5d5ed43d49b9aefde83d70b0 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---
title: Array.prototype.reverse()
slug: Web/JavaScript/Reference/Global_Objects/Array/reverse
tags:
  - Array
  - Array.prototype.reverse()
  - JavaScript
  - 原型
  - 数组
  - 方法
translation_of: Web/JavaScript/Reference/Global_Objects/Array/reverse
---
<div>{{JSRef}}</div>

<p><code><strong>reverse()</strong></code> 方法将数组中元素的位置颠倒,并返回该数组。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。该方法会改变原数组。</p>

<div>{{EmbedInteractiveExample("pages/js/array-reverse.html")}}</div>

<div></div>

<div></div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><code><var> arr</var>.reverse()</code></pre>

<h3 id="返回值">返回值</h3>

<p>颠倒后的数组。</p>

<h2 id="描述">描述</h2>

<p><code>reverse</code> 方法颠倒数组中元素的位置,改变了数组,并返回该数组的引用。</p>

<p>reverse方法是特意类化的;此方法可被 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call" title="The call() method calls a function with a given this value and arguments provided individually.">called</a> 或 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply" title="The apply() method calls a function with a given this value, and arguments provided as an array (or an array-like object).">applied</a>于类似数组对象。对象如果不包含反映一系列连续的、基于零的数值属性中的最后一个长度的属性,则该对象可能不会以任何有意义的方式运行。</p>

<h2 id="示例">示例</h2>

<h3 id="颠倒数组中的元素">颠倒数组中的元素</h3>

<p>下例将会创建一个数组 sourceArray,其包含三个元素,然后颠倒该数组。</p>

<p> <code>reverse()</code> 的调用返回了一个颠倒后的数组 <code>a</code>的引用。</p>

<pre><code>const a = [1, 2, 3];

console.log(a); // [1, 2, 3]

a.reverse();

console.log(a); // [3, 2, 1]</code></pre>

<h3 id="颠倒类数组中的元素">颠倒类数组中的元素</h3>

<p>下例创造了一个类数组对象 <code>a</code>, 包含3个元素和一个 length 属性, 然后颠倒这个类数组对象。  <code>reverse()</code> 的调用返回一个颠倒后的类数组对象 <code>a</code>的引用。</p>

<pre><code>const a = {0: 1, 1: 2, 2: 3, length: 3};

console.log(a); // {0: 1, 1: 2, 2: 3, length: 3}

Array.prototype.reverse.call(a); //same syntax for using apply()

console.log(a); // {0: 3, 1: 2, 2: 1, length: 3}</code></pre>

<h2 id="Specifications">Specifications</h2>

<table>
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.1.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.4.4.8', 'Array.prototype.reverse')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-array.prototype.reverse', 'Array.prototype.reverse')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.prototype.reverse', 'Array.prototype.reverse')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> <br>
     </td>
  </tr>
 </tbody>
</table>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.1</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.4.4.8', 'Array.prototype.reverse')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-array.prototype.reverse', 'Array.prototype.reverse')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>
<div>


<p>{{Compat("javascript.builtins.Array.reverse")}}</p>
</div>
</div>

<h2 id="See_also" name="See_also">相关链接</h2>

<ul>
 <li>{{jsxref("Array.prototype.join()")}}</li>
 <li>{{jsxref("Array.prototype.sort()")}}</li>
 <li>{{jsxref("TypedArray.prototype.reverse()")}}</li>
</ul>