WebGL Tutorial
and more

pop

撰写时间:2024-03-11

最新修订:2024-03-11

pop

将数组的最后一个元素移出数组。改变原数组。

原型

* | undefinedpop

参数

None

返回值

返回所移除的元素。如果数组为空,则返回undefined

说明

pop方法将数组的最后一个元素移出数组,并返回被移除的元素。而如果数组为空,则返回undefined

数组的length属性的数值随之而减一。

而与此方法相反的push方法则将可变长的元素添加至数组的末尾。应注意的是,pop方法只移除1个元素,而push方法可添加可变长的多个元素。

shift方法将数组的首个元素移出数组。

例子

原地移除

let nums = [3, 2, 5, 8]; let removed = nums.pop(); console.log(removed); // 8 console.log(nums); // [3, 2, 5]

pop方法将首个元素3移出数组,并返回所移除元素的引用。

移除后,原来的数组的内容,以及数组的length属性得以发生变化。

参见

  1. push
  2. shift

参考资源

  1. ECMA 262: Array Objects