[...nodelist]
will make an array of out of an object if the object is iterable.
Array.from(nodelist)
will make an array out of an object if the object is iterable or if the object is array-like (has .length
and numeric props)
Your two examples will be identical if NodeList.prototype[Symbol.iterator]
exists, because both cases cover iterables. If your environment has not been configured such that NodeList
is iterable however, your first example will fail, and the second will succeed. Babel
currently does not handle this case properly.
So if your NodeList
is iterable, it is really up to you which you use. I would likely choose on a case-by-case basis. One benefit of Array.from
is that it takes a second argument of a mapping function, whereas the first [...iterable].map(item => item)
would have to create a temporary array, Array.from(iterable, item => item)
would not. If you are not mapping the list however, it does not matter.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…