Not weird at all, it's a common pattern. That way, you can easily extend your range objects with other properties, like min, max, includes, etc.
range
min
max
includes
More importantly, it also ensures you don't accidentally mutate the range and get unexpected results. With your version:
const r = range(1, 10) ;[...r] // [1, 2, 3, 4, 5, 6, 7, 8, 9], as expected ;[...r] // [], empty array, because the iterator has already been consumed
With my version:
const r = range(1, 10) ;[...r] // [1, 2, 3, 4, 5, 6, 7, 8, 9], as expected ;[...r] // [1, 2, 3, 4, 5, 6, 7, 8, 9], same as before
You can still get a mutable iterator from my version if you really need one, though. You just have to ask for it explicitly:
const i = range(1, 10)[Symbol.iterator]() ;[...i] // [1, 2, 3, 4, 5, 6, 7, 8, 9] ;[...i] // []
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Not weird at all, it's a common pattern. That way, you can easily extend your
range
objects with other properties, likemin
,max
,includes
, etc.More importantly, it also ensures you don't accidentally mutate the
range
and get unexpected results. With your version:With my version:
You can still get a mutable iterator from my version if you really need one, though. You just have to ask for it explicitly: