--- title: Array.prototype.unshift() slug: Web/JavaScript/Reference/Global_Objects/Array/unshift tags: - Array - JavaScript - Method - 原型 - 参考资料 - 数组 - 方法 translation_of: Web/JavaScript/Reference/Global_Objects/Array/unshift --- <p>{{JSRef}}</p> <p><strong><code>unshift()</code></strong> 方法将一个或多个元素添加到数组的<strong>开头</strong>,并返回该数组的<strong>新长度(该</strong>方法修改原有数组<strong>)</strong>。</p> <div>{{EmbedInteractiveExample("pages/js/array-unshift.html")}}</div> <h2 id="Syntax" name="Syntax">语法</h2> <pre class="syntaxbox">arr.unshift(element1, ..., elementN) </pre> <h3 id="Parameters" name="Parameters">参数列表</h3> <dl> <dt><code>elementN</code></dt> <dd>要添加到数组开头的元素或多个元素。</dd> </dl> <h3 id="返回值">返回值</h3> <p>当一个对象调用该方法时,返回其 {{jsxref("Array.length", "length")}} 属性值。</p> <dl> </dl> <h2 id="Description" name="Description">描述</h2> <p><code>unshift</code> 方法会在调用它的类数组对象的开始位置插入给定的参数。</p> <p><code>unshift</code> 特意被设计成具有通用性;这个方法能够通过 {{jsxref("Function.call", "call")}} 或 {{jsxref("Function.apply", "apply")}} 方法作用于类数组对象上。<span style="line-height: inherit;">不过对于没有 length 属性(代表从0开始的一系列连续的数字属性的最后一个)的对象,调用该方法可能没有任何意义。</span></p> <p>注意, 如果传入多个参数,它们会被以块的形式插入到对象的开始位置,它们的顺序和被作为参数传入时的顺序一致。 于是,传入多个参数调用一次 <code>unshift</code> ,和传入一个参数调用多次 <code>unshift</code> (例如,循环调用),它们将得到不同的结果。例如:</p> <pre class="syntaxbox">let arr = [4,5,6]; arr.unshift(1,2,3); console.log(arr); // [<strong>1, 2, 3</strong>, 4, 5, 6] arr = [4,5,6]; // 重置数组 arr.unshift(1); arr.unshift(2); arr.unshift(3); console.log(arr); // [<strong>3, 2, 1</strong>, 4, 5, 6] </pre> <h2 id="示例">示例</h2> <pre class="brush: js">let arr = [1, 2]; arr.unshift(0); // result of the call is 3, which is the new array length // arr is [0, 1, 2] arr.unshift(-2, -1); // the new array length is 5 // arr is [-2, -1, 0, 1, 2] arr.unshift([-4, -3]); // the new array length is 6 // arr is [[-4, -3], -2, -1, 0, 1, 2] arr.unshift([-7, -6], [-5]); // the new array length is 8 // arr is [ [-7, -6], [-5], [-4, -3], -2, -1, 0, 1, 2 ] </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">规范</th> <th scope="col">状态</th> <th scope="col">备注</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.13', 'Array.prototype.unshift')}}</td> <td>{{Spec2('ES5.1')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ES6', '#sec-array.prototype.unshift', 'Array.prototype.unshift')}}</td> <td>{{Spec2('ES6')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-array.prototype.unshift', 'Array.prototype.unshift')}}</td> <td>{{Spec2('ESDraft')}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <div class="hidden">兼容性表是使用结构化数据生成的. 如果您希望为此数据做贡献, 请检出(check out) <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 同时向我们提交一个pull request.</div> <p>{{Compat("javascript.builtins.Array.unshift")}}</p> <h2 id="参见">参见</h2> <ul> <li>{{jsxref("Array.prototype.push()")}}</li> <li>{{jsxref("Array.prototype.pop()")}}</li> <li>{{jsxref("Array.prototype.shift()")}}</li> <li>{{jsxref("Array.prototype.concat()")}}</li> </ul>