In JavaScript, almost everything that has an iterable structure can go through some operations of the iteration engine, such as: for..of and ...spread
Any iterable if it returns an iterator for [[Get]] operations against the property in which it stores the @@iterator character, which in this case, apparently, an HTMLCollection returns such an object.
An iterator is considered on its own if it has: next() {method}: and two other optional methods, which are
return() {method}: stops iterator and returns IteratorResult throw() {method}: signals error and returns IteratorResult
This is an example of a complete user object that is iterable.
const calculations = { counting: 1, [Symbol.iterator]: function(){ return this; }, next: function(){ if(this.counting <= 3){ return { value: this.counting++, done: false } } return { value: this.counting, done: true} } }; const data = ...calculations;
So, in your case, as long as the HTMLCollection returns the correct iterator, you can apply for..of and ...spread , but if you for..of to this, it meets the specification than I have to tell you that I do not have a bachelor's degree in computer science : P
source share