/**
* 支持负数索引的数组
*/
function MinusArray(...items) {
return new Proxy([...items], {
get(target, index, proxyInstance) {
/* 索引归到正数区间 */
let length = target.length;
index = Number(index) % length;
index = index < 0 ? index + length : index
return target[index];
}
});
}
let arr = new MinusArray(1, 2, 3, 4, 5);
console.log('* arr', arr[-1]);