Skip to content

Latest commit

 

History

History
19 lines (15 loc) · 386 Bytes

File metadata and controls

19 lines (15 loc) · 386 Bytes
let array = [1, 2, 3];

array = new Proxy(array, {
  get(target, prop, receiver) {
    if (prop < 0) {
      // mesmo se acessarmos como arr[1]
      // prop é uma string, então é necessário convertê-la para um número.
      prop = +prop + target.length;
    }
    return Reflect.get(target, prop, receiver);
  }
});


alert(array[-1]); // 3
alert(array[-2]); // 2