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

You have some pretty unidiomatic JS in there. For example:

Array.prototype.filter.call(document.querySelectorAll(selector), filterFn);

What's more idiomatic is document.querySelectorAll(selector).filter(filterFn)

The same holds for forEach.

I suggest you look into how javascript prototype based OOP works



.filter() is not a method on the NodeList though, it only has forEach(), and even that is quite recent.

This is why people convert it to an array, and one of many reasons the standard JS API (and the DOM one in particular) are annoying to work with.


There are still more idiomatic methods you can use, ie:

    [...document.querySelectorAll(selector)].filter(filterFn);


That's a significantly newer syntax, so I wouldn't say it's "more idiomatic" than explicitly referencing the original function on the prototype (which is idiomatic to JS) and was the way to do it until the spread proposal, Array.from, and similar additions, what, ~5 years ago?

If anything isn't that code _less_ idiomatic in that it's less specific to JS and more of a generic operation?


In this instance I understood it to be idiomatic as it calls a JS Array constructor and iterates on the passed parameter to create it. It's been more common than the call/apply methods for years—at least as far as I've seen. The use of the array literal is always preferred, AFAIU.


I agree it'll probably become the new idiom in JS, but it probably needs another few years to begin taking over from the idiom that existed for decades before (and still works).

If you've seen that more common than call/apply for years then you probably work almost exclusively on new projects, with people that convert things to bleeding-edge, or with heavy transpilation: The spread operator as used there has only existed in regular released browsers/node for 3-4 years. I don't think it's a stretch to say the vast majority of code out there at this time won't be doing it that way.


Yeah could be. And my client base, as it were, are solely internal to the company I work for at this time and we have some knowledge and some cooperation over what versions of browsers people are using when it comes to web-based software. So support for older systems is largely unnecessary. That and building node backends the ES version doesn’t affect them anyway.

Definitely colours my work, and knowledge base.


I didn't make the site, it's a fork of the original website by Hubspot.




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

Search: