DEV Community

loading...

Discussion on: Why did Array.from()work?

Collapse
lucis profile image
Lucis • Edited

HTMLCollection may looks like an Array but it isn't, hence the Array.from() or [...copiedContents], since you'll need to transform it on an actual array for performing some array-like operations.

Doing so changes the iterable data structure: it is, indeed, not the original copiedContents, but the insides are the same DOM references to elements, you didn't make new ones. This article should help you understand it a bit.

Collapse
trainingmontage profile image
Jeff Caldwell Author

Got it. So, copying the HTMLCollection into a new array using Array.from() also copied all of the references to the original, thus changes to the copy reflected back to the DOM. That seems convenient and dangerous at the same time.

Collapse
lucis profile image
Lucis

Yeah, right!

This is the nature of working with DOM elements, don't know if I'm just used to it. It's very reference-y and with a bunch of side-effects, I believe that's why we use libs like React, since we almost don't have to deal with this.