push / unshift

push 向数组末尾添加一个或多个元素

var arr = [1, 2, 3];
arr.push(1);
var result = arr.push(1, 2, 3);
console.log(arr, result);

Untitled

unshift 向数组开头添加一个或多个元素

var arr = [1, 2, 3];
arr.unshift(1);
var result = arr.unshift(2, 3);
console.log(arr, result);

Untitled

自定义 push 方法

var arr = [1, 2, 3];

Array.prototype.selfPush = function() {
	var args = arguments;
  for (var i = 0; i < args.length; i++) {
  	this[this.length] = args[i];
  }
  
  return this.length;
}

var result = arr.selfPush(4, 5, 6);

console.log(arr, result);

Untitled

pop / shift

⭐️ pop 删除数组中最后一个元素