--- 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="语法" style="margin-bottom: 20px; line-height: 30px;">语法</h2> <pre class="syntaxbox"><code><var> arr</var>.reverse()</code></pre> <h3 id="返回值" style="line-height: 24px;">返回值</h3> <p>颠倒后的数组。</p> <h2 id="描述" style="margin-bottom: 20px; line-height: 30px;">描述</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="示例" style="margin-bottom: 20px; line-height: 30px;">示例</h2> <h3 id="颠倒数组中的元素" style="line-height: 24px;">颠倒数组中的元素</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="规范" style="margin-bottom: 20px; line-height: 30px;">规范</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="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容性</h2> <div> <div> <p>{{Compat("javascript.builtins.Array.reverse")}}</p> </div> </div> <h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2> <ul> <li>{{jsxref("Array.prototype.join()")}}</li> <li>{{jsxref("Array.prototype.sort()")}}</li> <li>{{jsxref("TypedArray.prototype.reverse()")}}</li> </ul>