reverse() 方法颠倒数组中元素的位置。第一个元素会成为最后一个,最后一个会成为第一个。
语法
arr.reverse()
参数
无
描述
reverse 方法颠倒数组中元素的位置,并返回该数组的引用。
示例
例子:颠倒数组中的元素
下例将会创建一个数组 myArray,其包含三个元素,然后颠倒该数组。
var myArray = ['one', 'two', 'three']; myArray.reverse(); console.log(myArray) // ['three', 'two', 'one']
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1 |
| ECMAScript 5.1 (ECMA-262) Array.prototype.reverse |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) Array.prototype.reverse |
Standard |
浏览器兼容性
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 1.0 | 1.0 (1.7 or earlier) | 5.5 | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |