Array.prototype.pop()删除一个数组中的最后的一个元素,并且返回这个元素。即,删除一个数组中的最后一个元素,并且把这个删除掉的元素返回给调用者。
//删除元素之前数组的内容为["angel", "clown", "mandarin", "surgeon"]
//以下代码输出结果为:myFish before: angel,clown,mandarin,surgeon
console.log("myFish before: " + myFish);
//调用删除数组最后一个元素方法,将返回值给popped
var popped = myFish.pop();
//删除最后一个元素之后,数组内容为["angel", "clown", "mandarin"]
//以下代码输出结果为:myFish after: angel,clown,mandarin
console.log("myFish after: " + myFish);
//以下代码,输出被删除的元素,输出结果为:Removed this element: surgeon
console.log("Removed this element: " + popped);
//例子: 删除掉数组的最后一个元素
//删除元素之前数组的内容为["angel", "clown", "mandarin", "surgeon"]
//以下代码输出结果为:myFish before: angel,clown,mandarin,surgeon
console.log("myFish before: " + myFish);
//调用删除数组最后一个元素方法,将返回值给popped
var popped = myFish.pop();
//删除最后一个元素之后,数组内容为["angel", "clown", "mandarin"]
//以下代码输出结果为:myFish after: angel,clown,mandarin
console.log("myFish after: " + myFish);
//以下代码,输出被删除的元素,输出结果为:Removed this element: surgeon
console.log("Removed this element: " + popped);