Array.prototype.pop()
Array.prototype.pop()
方法用于移除数组最后一个成员,并返回该数组成员。
语法
语法:
arr.pop();
类型声明:
interface Array<T> {
pop(): T | undefined;
}
返回值:
返回被移除的数组成员。如果该数组为空(没有任何元素),则返回 undefined
。
方法说明
- 该方法和
call()
或apply()
一起使用时,可应用在类数组对象上。pop
方法根据length
属性来确定最后一个元素的位置。如果不包含length
属性或length
属性不能被转成一个数值,会将length
置为 0,并返回undefined
。 - 由于该方法会移除数组中的最后一个元素,数组的
length
属性也会随之改变(如果数组中有元素的话),一般而言,数组的length
属性将会减 1。 - 如果你在一个空数组上调用
pop()
,它返回undefined
。
代码示例
基本用法
let foo = ['a', 'b', 'c', 'd'];
let popped = foo.pop();
console.log(foo);
// ['a', 'b', 'c', 'd']
console.log(popped);
// 'd'
在空数组中调用
let empty = [];
let popped = empty.pop();
console.log(popped);
// undefined
console.log(empty);
// []