Created
February 25, 2019 09:43
-
-
Save Sparkenstein/d5c5b0e00108c51a7d121749bd9cc4db to your computer and use it in GitHub Desktop.
Symbol.iterator on number prototype
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
const { log } = console; | |
Number.prototype[Symbol.iterator] = function () { | |
let i = 0; | |
const end = this; | |
return { | |
next() { | |
return { | |
value: i++, | |
done: i > end, | |
}; | |
}, | |
}; | |
}; | |
// for-of | |
for (i of 2) { | |
log(i); | |
} | |
// array-destructuring | |
const [, a] = 10; | |
log(a) | |
// spread operator | |
const a = [...10] | |
log(a) | |
// rest parameters | |
function test(...a){ | |
log(a) | |
} | |
test(...3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment