此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
Array.prototype.pop()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
pop() 方法从数组中删除最后一个元素,并返回该元素的值。此方法会更改数组的长度。
In this article
尝试一下
const plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"];console.log(plants.pop());// Expected output: "tomato"console.log(plants);// Expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]plants.pop();console.log(plants);// Expected output: Array ["broccoli", "cauliflower", "cabbage"]语法
pop()返回值
从数组中删除的元素(当数组为空时返回undefined)。
描述
pop() 方法从一个数组中删除并返回最后一个元素给调用者。如果你在空数组上调用pop(),它会返回undefined。
Array.prototype.shift() 和pop() 有类似的行为,但是它是作用在数组的第一个元素上的。
pop() 是修改方法。其改变了this 的长度和内容。如果你想要this 不变,但是返回一个新的最后一个元素被移除的数组,你可以使用arr.slice(0, -1) 来代替。
pop() 方法是通用的。它只期望this 值具有length 属性和整数键属性。虽然字符串也是类数组对象,但是由于其不能被修改,所以pop() 方法并不能应用在字符串上。
示例
>删除掉数组的最后一个元素
下面的代码首先创建了一个拥有四个元素的数组myFish,然后删除掉它的最后一个元素。
const myFish = ["angel", "clown", "mandarin", "sturgeon"];const popped = myFish.pop();console.log(myFish); // ['angel', 'clown', 'mandarin' ]console.log(popped); // 'sturgeon'在非数组对象上调用 pop()
pop() 方法会读取this 上的length 属性。如果规范化的 length 属性为 0,length 会被再次设置为 0(鉴于之前可能是负数或者undefined)。否则,返回并删除位于length - 1 处的属性。
const arrayLike = { length: 3, unrelated: "foo", 2: 4,};console.log(Array.prototype.pop.call(arrayLike));// 4console.log(arrayLike);// { length: 2, unrelated: 'foo' }const plainObj = {};// 没有 length 属性,所以长度为 0Array.prototype.pop.call(plainObj);console.log(plainObj);// { length: 0 }以类数组的方式使用对象
push 和pop 方法是通用的,我们可以利用这一点来编写更灵活的代码——如以下示例所示。
请注意,在此示例中,我们不会创建数组来存储对象集合。相反,我们将集合存储在对象本身上,并在Array.prototype.push 和Array.prototype.pop 上使用call 来欺骗这些方法,让它们认为我们正在处理一个数组。
const collection = { length: 0, addElements(...elements) { // 每次添加元素时 // obj.length 都会自动增加 // 返回 push 方法的返回值,即 length 属性的新值 return [].push.call(this, ...elements); }, removeElement() { // 每次移除元素时 // obj.length 都会自动减少 // 返回 pop 方法的返回值,即被移除的元素 return [].pop.call(this); },};collection.addElements(10, 20, 30);console.log(collection.length); // 3collection.removeElement();console.log(collection.length); // 2规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-array.prototype.pop> |