函数特点
- 必须有length属性和以整数为key的属性
函数语法
// index 要返回数组的元素索引(位置),传递负数时,从数组末端开始倒数
// 返回给定索引在数组中的元素,找不到则返回undefined
Array.prototype.at(index)
函数使用场景
- 返回数组最后一个元素
// 数组
const ary = [1,2,3,4,5];
ary.at(-1); // 5
// 类数组
const aryLike = {
length: 2,
0: 'a',
1: 'b',
};
Array.prototype.at.call(aryLike, -1) // 'b'
// 字符串
const str = '12345';
Array.prototype.at.call(str, -1); // '5'