Re:The Brutal Lifecycle of JavaScript Frameworks
by sim0n on Thursday January 11, 2018 @04:47PM
(#16128245)
DOM APIs have much improved since jQuery first arrived, so while that was the case originally, it's become less true over time. It's definitely still less verbose though, for example:
vs.
[ Parent
| on HN ]
Re:The Brutal Lifecycle of JavaScript Frameworks
by esprehn on Friday January 12, 2018 @12:42AM
(#16130656)
The modern DOM API has forEach on NodeList so you can skip the Array.from.
document.querySelectorAll('.my-component div').forEach(node => node.style.background = 'yellow');
https://developer.mozilla.org/en-US/docs/Web/API/NodeList/fo...
Re:The Brutal Lifecycle of JavaScript Frameworks
by scottmf on Thursday January 11, 2018 @06:01PM
(#16128873)
Why use map instead of forEach?
Re:The Brutal Lifecycle of JavaScript Frameworks
by tvalentius on Thursday January 11, 2018 @08:27PM
(#16129701)
If you only want to translate(map) each element in to another set of value, using Map is much more simpler and readable than forEach
Re:The Brutal Lifecycle of JavaScript Frameworks
by grumblestumble on Friday January 12, 2018 @01:27PM
(#16135065)
Actually, this is incorrect. You're abusing map here. The purpose of map is to map an existing array onto a new array. You're using map here to mutate properties on nodes in the original array, and creating a new Array, which has the length of the initial array, where each value is the return value of `node.style.background = 'yellow';`
forEach is much more indicative of what you're actually doing here, which is running through an iterable and mutating properties on each node.
Simple rule of thumb: if you're not using the results of `map`, you shouldn't be using it.