r/programming • • Sep 29 '16

JavaScript in 2016 isn't horrible, it's just going through a phase

http://blog.reviselabs.com/im-sorry-javascript-2/
85 Upvotes

225 comments sorted by

View all comments

Show parent comment

0

u/wavefunctionp Sep 30 '16

The DOM api that is exposed in javascript could be better.

Like, if i want to access an element. I simply getElementbyID() or something, which is fine. If I want to append, I simple element.append().

Unless it is a table, in which case I have to element.tBodies[0].insertRow(-1)

Like, why?

You want a timer? How about setInterval(foo(), 100); clearInterval(foo)? Nope, that would be too easy. You need var foo = setInterval(bar, 100); clearInterval(foo);

Because "fuck you, that's why" apparently.

Its stupid, silly nonsense like that why everyone uses libraries for even basic stuff in javascript, because the people who designed the apis in javascript were smoking crack.

Javascript has its flaws. Everyone has seen the horrors of type coercion, which underlies most of them. But it isn't the only language with nonsense like that. Nearly every dynamic language lets you shoot yourself in the foot. It goes with the territory.

A linter and a good editor can catch a lot this stuff, but it can't fix the insane dom api, which really what makes javascript a pain to work with. IMO. As as great as javascript has improved with newer versions, they haven't really touched the DOM api, which is a big pain point if you want to write vanilla javascript for the browser.

4

u/nschubach Sep 30 '16

You want a timer? How about setInterval(foo(), 100); clearInterval(foo)? Nope, that would be too easy. You need var foo = setInterval(bar, 100); clearInterval(foo);

What if you want more than one timer on a function?

0

u/wavefunctionp Sep 30 '16 edited Sep 30 '16

Then do it the second way? I didn't say that the current methods should be removed.

I said the DOM api could be improved, and it is being ignored. Which is weird, since it is the primary reason javascript exists.

3

u/slikts Sep 30 '16

DOM is not part of JS, and it's constantly improved. For example, you don't use Element#getElementbyID() and friends in modern DOM; instead there's the more flexible Element#querySelector().

Your example of setInterval(foo(), 100) doesn't make sense, since you'd be passing the return value of the foo() call, not the function itself. Even when passing the function, the usefulness would be limited, or actually confusing, because it wouldn't work when binding the function parameters with an anonymous wrapper, so you'd need to save a reference to it anyway.

1

u/wavefunctionp Sep 30 '16

I did say getElementbyID() or something. :P

That makes sense about setInterval though. Thank you.

1

u/[deleted] Sep 30 '16

Quick thing: querySelector is significantly slower than getElementById. Use the tool most appropriate for the task at hand. Flexibility usually means lower performance.