Live data from Hacker News

How true hackers write JavaScript

news.ycombinator.com

31–40 of 342 posts

Re: How true hackers write JavaScript

#31
post #4

That might be one of the most readable pieces of code that I've ever read.

Really? It has horrendous naming and it isn't even using es6 function byClass (el, cl) { return el ? el.getElementsByClassName(cl) : [] } const byClass = (el, cl) => el ? el.getElementsByClassName(cl) : []

The es6 syntax is LESS readable. It's less declarative. Just quicker to type if you don't have a nice editor setup.

My issue with this method is the name more than anything. I would also guess if it's even needed - probably bad application logic is requiring that ternary statement but didn't read it yet.

Ideal method name is already given - getEmementsByClassName... Really should just have a utility to default return types.

Re: How true hackers write JavaScript

#33
post #11

This is neat (how the up/downvote onclick handler sends the info to the server). new Image().src = el.href; Where href looks like this: vote?id=xxxxxxxx&how=up&auth=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy&goto=item%3Fid%3Dzzzzzzzz#wwwwwwww

Maybe it's neat but this is not how anyone would write it today. This is the legacy code still left from the time before ajax was invented.

Probably. I wonder though if they are trying not to use any libraries and Image was just easier than xhr.

Re: How true hackers write JavaScript

#35
post #11

This is neat (how the up/downvote onclick handler sends the info to the server). new Image().src = el.href; Where href looks like this: vote?id=xxxxxxxx&how=up&auth=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy&goto=item%3Fid%3Dzzzzzzzz#wwwwwwww

Maybe it's neat but this is not how anyone would write it today. This is the legacy code still left from the time before ajax was invented.

Hacker News was launched 2007.

XMLHttpRequest has been around for about 7 years then and using it was called Ajax for about 2 years already.

Re: How true hackers write JavaScript

#36
post #29

Earlier quoted context omitted.

Really? It has horrendous naming and it isn't even using es6 function byClass (el, cl) { return el ? el.getElementsByClassName(cl) : [] } const byClass = (el, cl) => el ? el.getElementsByClassName(cl) : []

How does es6 improve readability in this case? It barely removes `{}` and replaces very clear word `function` with `const` that in most languages is associated with constant values, not with functions

It removes the brackets and the return keyword, const is in fact a constant but this not ES6 it is just doing a function expression instead a function declaration (you create a const variable and assign an anonymous function to it instead of using the keyword 'function')

This is used for readability and to avoid hoisting that can be confusing.

Re: How true hackers write JavaScript

#37

It only exists because DOM APIs are crap, and jQuery is too big. So it ends up reimplementing parts of jQuery needed to run.

For the record, jQuery 3.3.1 (production, normal) is at 30kb [1]; if you don't want effects (production, slim), you can get it for less than 24kb [2]. For the value it provides, that does not sound "too big" to me.

[1]: https://code.jquery.com/jquery-3.3.1.min.js [2]: https://code.jquery.com/jquery-3.3.1.slim.min.js

Re: How true hackers write JavaScript

#39
> function addClass (el, cl) { if (el) { var a = el.className.split(' '); if (!afind(cl, a)) { a.unshift(cl); el.className = a.join(' ')}} }

Why does it use `unshift` rather than `push` ?

The only reason I can think of is that the last class added will be faster to remove when iterating over the array...

Post reply on HN