Array.prototype.pop()

Array 实例的 pop() 方法从数组中删除最后一个元素并返回该元素。该方法改变数组的长度。

¥The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.

Try it

语法

¥Syntax

js
pop()

参数

¥Parameters

没有任何。

¥None.

返回值

¥Return value

从数组中删除的元素;如果数组为空,则为 undefined

¥The removed element from the array; undefined if the array is empty.

描述

¥Description

pop() 方法从数组中删除最后一个元素并将该值返回给调用者。如果对空数组调用 pop(),它将返回 undefined

¥The pop() method removes the last element from an array and returns that value to the caller. If you call pop() on an empty array, it returns undefined.

Array.prototype.shift()pop() 具有类似的行为,但应用于数组中的第一个元素。

¥Array.prototype.shift() has similar behavior to pop(), but applied to the first element in an array.

pop() 方法是一种变异方法。它改变了 this 的长度和内容。如果你希望 this 的值相同,但返回删除最后一个元素的新数组,则可以使用 arr.slice(0, -1) 代替。

¥The pop() method is a mutating method. It changes the length and the content of this. In case you want the value of this to be the same, but return a new array with the last element removed, you can use arr.slice(0, -1) instead.

pop() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。虽然字符串也是类似数组的,但此方法不适合应用于它们,因为字符串是不可变的。

¥The pop() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

示例

¥Examples

删除数组的最后一个元素

¥Removing the last element of an array

以下代码创建包含四个元素的 myFish 数组,然后删除其最后一个元素。

¥The following code creates the myFish array containing four elements, then removes its last element.

js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];

const popped = myFish.pop();

console.log(myFish); // ['angel', 'clown', 'mandarin' ]

console.log(popped); // 'sturgeon'

对非数组对象调用 pop()

¥Calling pop() on non-array objects

pop() 方法读取 thislength 属性。如果 标准化长度 为 0,则 length 再次设置为 0(而之前可能为负数或 undefined)。否则,返回 length - 1 处的属性,并返回 deleted 处的属性。

¥The pop() method reads the length property of this. If the normalized length is 0, length is set to 0 again (whereas it may be negative or undefined before). Otherwise, the property at length - 1 is returned and deleted.

js
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }

以类似数组的方式使用对象

¥Using an object in an array-like fashion

pushpop 是故意通用的,我们可以利用它来发挥我们的优势 - 正如下面的示例所示。

¥push and pop are intentionally generic, and we can use that to our advantage — as the following example shows.

请注意,在此示例中,我们没有创建数组来存储对象集合。相反,我们将集合存储在对象本身上,并在 Array.prototype.pushArray.prototype.pop 上使用 call 来欺骗这些方法,让它们认为我们正在处理一个数组。

¥Note that in this example, 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 and Array.prototype.pop to trick those methods into thinking we're dealing with an array.

js
const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  },
};

collection.addElements(10, 20, 30);
console.log(collection.length); // 3
collection.removeElement();
console.log(collection.length); // 2

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.pop

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看