Skip to content

Commit 87936a4

Browse files
Josh68spaceninja
authored andcommitted
Change note for transforming NodeLists to Arrays
Found that the issue is not needing to use a `for ... of` loop, but to add in `@babel/plugin-transform-spread` (whose default option is `loose:false`, which will override a global `loose:true` on `@babel/preset-env`). This transformation works correctly, including when using a `.browserlistrc` that includes `IE 11`. Thanks @calebeby.
1 parent 20a7d7e commit 87936a4

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

javascript/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,6 @@ const animalsCopy = [...animals];
426426
427427
To convert an iterable object (e.g. [NodeList]) to an array, use [array spread syntax][Array Literal Spread Syntax] `...` instead of [Array.from].
428428
429-
(Note: This may not work correctly if transpiling with support for IE11. Instead, try a `for ... of` loop over the `NodeList`.)
430-
431429
> Why? Better performance.
432430
433431
#### Examples
@@ -445,6 +443,19 @@ const nodes = Array.from(paragraphs);
445443
const paragraphs = document.querySelectorAll('p');
446444
const nodes = [...paragraphs];
447445
```
446+
#### Note
447+
448+
If using Babel with `@babel/preset-env` with option `loose:true`, and are transpiling to older targets in a `.browserlistrc`, you may need to add the following to your Babel config (e.g., `babel.config.js`):
449+
450+
```js
451+
plugins: [
452+
...,
453+
'@babel/plugin-transform-spread',
454+
...
455+
]
456+
```
457+
458+
(The default option for this plugin is `loose:false`, which will override the global setting)
448459

449460
#### Resources
450461

0 commit comments

Comments
 (0)