Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Very true, nice codes :). Would be nice if we could write that without transpiling.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: