mozilla

Revision 542145 of Array.prototype.shift()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/shift
  • Revision title: Array.prototype.shift()
  • Revision id: 542145
  • Created:
  • Creator: endlesswind
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Array")}}

概述

shift() 方法删除一个数组的第一个元素并返回这个元素. 该方法会改变数组的长度.

语法

arr.shift()

描述

shift 方法移除位于第0个索引的元素并连续降低之后的索引值(shifts the values at consecutive indexes down), 并返回被移除的元素值. 如果 length 属性值为0, 返回{{jsxref("undefined")}} .

shift 被有意的设计成通用(generic); 该方法可以像作用于数组一样 {{jsxref("Function.call", "called")}} or {{jsxref("Function.apply", "applied")}} 作用于对象上. 当对象不包含 length 属性时, 无意义的0值length属性会被添加(Objects which do not contain a length property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner).

示例

示例: 移除一个数组中的一个元素

以下代码打印了移除第一个元素之前和之后数组 myFish. 还打印了被移除的元素:

var myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log("myFish before: " + myFish);

var shifted = myFish.shift();

console.log("myFish after: " + myFish);
console.log("Removed this element: " + shifted);

这个例子显示如下:

myFish before: angel,clown,mandarin,surgeon
myFish after: clown,mandarin,surgeon
Removed this element: angel

规范

Specification Status Comment
ECMAScript 3rd Edition Standard Initial definition. Implemented in JavaScript 1.2
{{SpecName('ES5.1', '#sec-15.4.4.9', 'Array.prototype.shift')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-array.prototype.shift', 'Array.prototype.shift')}} {{Spec2('ES6')}}  

浏览器兼容

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.7")}} 5.5 {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

参见

  • {{jsxref("Array.prototype.push()")}}
  • {{jsxref("Array.prototype.pop()")}}
  • {{jsxref("Array.prototype.unshift()")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Array")}}</div>
<h2 id="Summary" name="Summary">概述</h2>
<p><code><strong>shift()</strong></code>&nbsp;方法删除一个数组的第一个元素并返回这个元素. 该方法会改变数组的长度.</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">
<code><em>arr</em>.shift()</code></pre>
<h2 id="Description" name="Description">描述</h2>
<p><code>shift</code>&nbsp;方法移除位于第0个索引的元素并连续降低之后的索引值(shifts the values at consecutive indexes down), 并返回被移除的元素值. 如果&nbsp;<code>length</code>&nbsp;属性值为0, 返回{{jsxref("undefined")}} .</p>
<p><code>shift</code>&nbsp;被有意的设计成通用(generic); 该方法可以像作用于数组一样 {{jsxref("Function.call", "called")}} or {{jsxref("Function.apply", "applied")}} 作用于对象上. 当对象不包含 length 属性时, 无意义的0值length属性会被添加(Objects which do not contain a <code>length</code> property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner).</p>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example:_Removing_an_element_from_an_array" name="Example:_Removing_an_element_from_an_array">示例: 移除一个数组中的一个元素</h3>
<p>以下代码打印了<span style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;">移除第一个元素之前和之后数组</span><span style="line-height: 1.5;">&nbsp;</span><code style="font-size: 14px;">myFish</code><span style="line-height: 1.5;">. 还打印了被移除的元素:</span></p>
<pre class="brush:js">
var myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log("myFish before: " + myFish);

var shifted = myFish.shift();

console.log("myFish after: " + myFish);
console.log("Removed this element: " + shifted);
</pre>
<p>这个例子显示如下:</p>
<pre class="brush:js">
myFish before: angel,clown,mandarin,surgeon
myFish after: clown,mandarin,surgeon
Removed this element: angel
</pre>
<h2 id="Specifications">规范</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>ECMAScript 3rd Edition</td>
   <td>Standard</td>
   <td>Initial definition. Implemented in JavaScript 1.2</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.4.4.9', 'Array.prototype.shift')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-array.prototype.shift', 'Array.prototype.shift')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">浏览器兼容</h2>
<div>
 {{CompatibilityTable}}</div>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>1.0</td>
    <td>{{CompatGeckoDesktop("1.7")}}</td>
    <td>5.5</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">参见</h2>
<ul>
 <li>{{jsxref("Array.prototype.push()")}}</li>
 <li>{{jsxref("Array.prototype.pop()")}}</li>
 <li>{{jsxref("Array.prototype.unshift()")}}</li>
</ul>
<!--languages({
"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/shift"
})-->
Revert to this revision