Hacker News new | past | comments | ask | show | jobs | submit login

Array.from(document.getElementBy...).forEach ?



There are ways to do it for sure, but imagine if there was just One Obvious Way To Do It.


Don't get me going on Python in the browser... :)


I can't find a reference but I remember hearing a tale that Guido was contacted by Netscape just before Brendan Eich but wasn't interested. What an interesting parallel reality that conjours up.

Hmmmm. On second thoughts maybe we're be living in a world where Python was as maligned as Javascript.


1) There's certainly not "one way" to iterate in Python[1], and they're not even consistently named (xrange is the iterator version of range, but izip is the iterator version of zip).

2) You can use Python in the browser[2][3][4 sort of].

1. https://youtu.be/OSGv2VnC0go?t=3m20s

2. http://transcrypt.org

3. http://pyjs.org

4. http://www.rapydscript.com


From "import this"

> There should be one-- and preferably only one --obvious way to do it.

Key words are preferably and obviously. Of course there are going to be multiple ways to do everything, but it'd be nice if the best practice is pretty obvious.

> Although that way may not be obvious at first unless you're Dutch.


document.querySelectorAll(..).forEach?!?!?!?!


In Firefox, Safari, IE, and Chrome < 51:

    TypeError: document.querySelectorAll(...).forEach is not a function
That's exactly what this sub-thread is about! NodeList doesn't have .forEach.


Yep, that just rolls of the tongue...


Alias `document.querySelectorAll` to `$$`?


var $ = selector => [].slice.call(document.querySelectorAll(selector));

Fo' sizzle


If you're using ES6 you may as well use spread and defaults to make it more useful:

const $ = (selector, context=document) => [...context.querySelectorAll(selector)]


Or just polyfill it with NodeList.prototype.forEach = Array.prototype.forEach and get nice readable code.

Point is: we shouldn't have to.

Vanilla JS also allowed you to look inside HTML5 FormData a few months earlier. And no, it isn't FormData.toString().




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

Search: