JavaScript · Middle · Question 28 of 75
What does your own object need to work with for...of and spread?
- ✓Correct answer: Implement a
[Symbol.iterator]()method that returns an iterator - 2Add a
lengthproperty and numeric keys, like an array - 3Inherit the object from
Array.prototypeviaObject.create
Explanation
The iteration protocol: [Symbol.iterator] returns an object with next() that yields { value, done }. The easiest implementation is a generator. Objects with length are array-likes: they don't work with for...of and are converted with Array.from.