Skip to content

Instantly share code, notes, and snippets.

@getify
Created January 25, 2017 23:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save getify/49ae9a1f2a6031d40f5deb5ea25faa62 to your computer and use it in GitHub Desktop.
Save getify/49ae9a1f2a6031d40f5deb5ea25faa62 to your computer and use it in GitHub Desktop.
Make JavaScript Great Again
Object.defineProperty(Number.prototype,Symbol.iterator,{
*value({ start = 0, step = 1 } = {}) {
var inc = this > 0 ? step : -step;
for (let i = start; Math.abs(i) <= Math.abs(this); i += inc) {
yield i;
}
},
enumerable: false,
writable: true,
configurable: true
});
[ ...8 ]; // [0,1,2,3,4,5,6,7,8]
[ ...5[Symbol.iterator]({ start: 3 }) ]; // [3,4,5]
[ ...10[Symbol.iterator]({ start: 2, step: 4 }) ]; // [2,6,10]
[ ...-3 ]; // [0,-1,-2,-3]
[ ...(-3)[Symbol.iterator]({ start: 3, step: 2 }) ]; // [3,1,-1,-3]
@CharlyJazz
Copy link

wow 👍

@matthias-ccri
Copy link

What is this black magic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment