-
-
Save hamlim/4b8c4b9907c0c68b971955735fc10cc9 to your computer and use it in GitHub Desktop.
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
Number.prototype.to = function*(n) { | |
let finish = n.valueOf(); | |
let start = this.valueOf(); | |
let finishLessThan = start >= finish; | |
for ( | |
let i = start; | |
finishLessThan ? i >= finish: i <= finish; | |
i += finishLessThan ? -1 : +1 | |
) yield i; | |
}; | |
Object.setPrototypeOf(Number.prototype, new Proxy({}, { | |
get: (_, finish, start) => start.to(finish) | |
})); | |
// Loop from 20 to 200 | |
for (let n of 20[200]) {} | |
// Loop from -1000 to 0 (Neg must be wrapped in paren) | |
for (let n of (-1000)[0]) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment