查看Array.prototype.slice()。
当您调用array.slice() 时,将返回您数组的一个切片(作为另一个数组)。
var array = ['zero', 'one', 'two', 'three'];
// Grab the elements from `array`
// beginning at 1 and up to (not including) 3.
var sliced = array.slice(1, 3);
console.log(array); // ['zero', 'one', 'two', 'three']
console.log(sliced); // ['one', 'two']
console.log(sliced[0]) // 'one'
在您的代码中,您正在执行array.slice(-1)。文档说“可以使用 [a] 负索引,表示距序列末尾的偏移量。 slice(-2) 提取序列中的最后两个元素。”因此,您的array.slice(-1) 将返回一个新数组,其中填充了原始数组的最后一个元素array。
var array = ['zero', 'one', 'two', 'three'];
var sliced = array.slice(-1);
console.log(array); // ['zero', 'one', 'two', 'three']
console.log(sliced); // ['three']
console.log(sliced[0]); // 'three'
// All together, it looks like this.
// I'm using `alert()` instead of `console.log()`
// to mirror your code.
alert(array.slice(-1)[0]); // 'three'