Created
May 25, 2016 08:56
-
-
Save tdd/6f21695bfba161ecdfb2ebdd47f9c122 to your computer and use it in GitHub Desktop.
Negative indices on arrays using ES2015 proxies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function yay(array) { | |
return new Proxy(array, { | |
get (target, property, receiver) { | |
if (property < 0) { | |
property = target.length + Number(property) | |
} | |
return Reflect.get(target, property, receiver) | |
} | |
}) | |
} | |
// Demo use: | |
const arr = yay(['foo', 'bar', 'baz']) | |
arr[-1] // => 'baz' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a reminder, Babel won't transpile proxies: they can't be emulated in ES5. But you do have enough native support for this, as of May 25 2016, in Edge 12+, Fx38+, Ch49+, Saf TP, and Node 6.