Skip to content

Instantly share code, notes, and snippets.

@hosam1696
Forked from rauschma/range_via_proxy.js
Created June 24, 2018 15:55
Show Gist options
  • Save hosam1696/14f61e6c4a938a4bea7df4a59e45b5b2 to your computer and use it in GitHub Desktop.
Save hosam1696/14f61e6c4a938a4bea7df4a59e45b5b2 to your computer and use it in GitHub Desktop.
const handler = {
get(target, propKey, receiver) {
if (/^_[0-9]+$/.test(propKey)) {
const result = [];
const first = Number(receiver);
const last = Number(propKey.slice(1));
for (let i=first; i<=last; i++) {
result.push(i);
}
return result;
}
return Reflect.get(target, propKey, receiver);
}
};
const proxy = new Proxy(Object.prototype, handler);
Object.setPrototypeOf(Number.prototype, proxy);
/*
> 1 . _3
[ 1, 2, 3 ]
> 11 . _14
[ 11, 12, 13, 14 ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment