loading...

re: How it works - Custom spread operator [...5] VIEW POST

FULL DISCUSSION
 

Very interesting take on using iterator on numbers! Thanks for the article!

A suggestion: could have just used a generator for Number.prototype[Symbol.iterator].

//with recursion
Number.prototype[Symbol.iterator] = function*() {
  if (this <= 0) return
  if (this === 1) return 1
  yield* (this - 1)
  yield Number(this)
};

// without recursion
Number.prototype[Symbol.iterator] = function*() {
  if (this <= 0) return
  for (let i = 1; i <= this; i++) {
    yield Number(i)
  }
};
 

Thanks!

could have just used a generator for Number.prototype[Symbol.iterator] Yes i agree i could have used generator then i have to explain how that works to in the same article.

Idea of how it works is reading the specs and implementing how it could have been implemented if we write it in vanilla js.

 

Generators are still vanilla javascript. But I understand your concerns about the need to explain how generators work.

Nice one anayways! 👍

Yep. That's right! It is vanilla js but i wanted it to be simple as possible and didnt want to explain how generator works.

code of conduct - report abuse