MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

Array.prototype.push()

翻譯不完整。請協助 翻譯此英文文件

push() 方法會添加一個或多個元素至陣列的末端,並且回傳陣列的新長度。

語法

arr.push(element1, ..., elementN)

參數

elementN
欲添加至陣列的元素。

回傳值

The new length property of the object upon which the method was called.

描述

push 方法會把數值增加到陣列。

push is intentionally generic. This method can be used with call() or apply() on objects resembling arrays. The push method relies on a length property to determine where to start inserting the given values. If the length property cannot be converted into a number, the index used is 0. This includes the possibility of length being nonexistent, in which case length will also be created.

The only native, array-like objects are strings, although they are not suitable in applications of this method, as strings are immutable.

範例

將複數個元素添加至陣列

以下的程式碼會建立含有兩個元素的陣列 sports,接著再增加兩個元素進來。新結果的長度以變數 total 表示。

var sports = ['soccer', 'baseball'];
var total = sports.push('football', 'swimming');

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total);  // 4

合併兩個陣列

This example uses apply() to push all elements from a second array.

var vegetables = ['parsnip', 'potato'];
var moreVegs = ['celery', 'beetroot'];

// Merge the second array into the first one
// Equivalent to vegetables.push('celery', 'beetroot');
Array.prototype.push.apply(vegetables, moreVegs);

console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']

Using an object in an array-like fashion

As mentioned above, push is intentionally generic, and we can use that to our advantage. Array.prototype.push can work on an object just fine, as this example shows. Note that we don't create an array to store a collection of objects. Instead, we store the collection on the object itself and use call on Array.prototype.push to trick the method into thinking we are dealing with an array, and it just works, thanks to the way JavaScript allows us to establish the execution context however we please.

var obj = {
    length: 0,

    addElem: function addElem (elem) {
        // obj.length is automatically incremented every time an element is added.
        [].push.call(this, elem);
    }
};

// Let's add some empty objects just to illustrate.
obj.addElem({});
obj.addElem({});
console.log(obj.length);
// → 2

Note that although obj is not an array, the method push successfully incremented obj's length property just like if we were dealing with an actual array.

規範

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.2.
ECMAScript 5.1 (ECMA-262)
The definition of 'Array.prototype.push' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Array.prototype.push' in that specification.
Standard  
ECMAScript 2016 Draft (7th Edition, ECMA-262)
The definition of 'Array.prototype.push' in that specification.
Draft  

瀏覽器支援度

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)

閱讀更多

文件標籤與貢獻者

標籤: 
 此頁面的貢獻者: iigmir, ALiangLiang
 最近更新: iigmir,