Array.unshift

Did you know that you can read content offline by using one of these tools? If you would like to read offline MDN content in another format, let us know by commenting on Bug 665750.

Dash App
  • Tags
  • 文件

Summary

在数组的开始位置添加一个或者多个成员,并返回新的数组成员个数

数组方法
实现于 JavaScript 1.2
ECMAScript 版本 ECMAScript 3rd Edition

语法

数组名.unshift(成员1, ..., 成员N) 

参数列表

成员1, ..., 成员N
要添加到该数组开始位置的成员.

返回值

调用该方法的数组对象的length属性(也就是执行完该方法后数组的新的成员个数).

描述

unshift 方法会在调用它的数组对象的开始位置插入给定的参数,作为数组的新成员。

unshift 特意被设计成通用型的方法;这个方法能够对类似于数组的对象执行 called 或者 applied. 不过对于没有length属性(代表从0开始的一系列连续的数字属性的最后一个)的对象,调用该方法可能没有任何意义。

例子

var arr = [1, 2];

arr.unshift(0); //result of call is 3, the new array length
//arr is [0, 1, 2]

arr.unshift(-2, -1); // = 5
//arr is [-2, -1, 0, 1, 2]

arr.unshift( [-3] );
//arr is [[-3], -2, -1, 0, 1, 2]

See also