Given Array.from takes an iterable or array-like object as it's first argument, why does it not throw an error when passed an empty object, like this:
let emptyObject = Array.from( {} )
At iterable is defined as an object that implements the iterable protocol or the iterator protocol. An empty object seemingly does neither:
let emptyObject = {}
console.log(emptyObject[Symbol.iterator])
console.log(emptyObject['next'])
console.log(Array.from(emptyObject))
An Array-like object has a length property and can be indexed (ref). An empty object does not have a length property.
let emptyObject = {}
console.log(emptyObject['length'])
console.log(Array.from(emptyObject))
How does an empty object qualify?
Array.from()works with array-like objects. More specifically it only really needs thelengthproperty:This is defined in starting at step 7. of the Array.from algorithm
LengthOfArrayLikeis only this step:Which means to retrieve the value of the
lengthproperty of an object and defer toToLengthto convert it to a number.ToLengthis defined as the following: