4 ms·
You can create a trivial helper function such as: let $ = s => document.querySelectorAll(s); And then use: for (let p of $('p')) p.style.color
by megous 9y ago
You can create a trivial helper function such as:
let $ = s => document.querySelectorAll(s);
And then use:
for (let p of $('p'))
p.style.color = 'blue';
And if you start doing anything more complex per iteration:
$('p')
.css('color', 'blue')
.addClass('bla')
.filter(function() { $(this).parent().is('article'); })
.removeClass('qwe');
The code becomes much less magical and clearer (it's almost self explanatory):
for (let p of $('p')) {
p.style.color = 'blue';
p.classList.add('ble');
if (p.parentNode.matches('article'))
p.classList.remove('qwe');
}
Also much more debuggable in a debugger, and faster, as you're iterating only once and are not calling many needless functions.
- g00gler 9y agoVery true, nice codes :). Would be nice if we could write that without transpiling.