Live data from Hacker News

How true hackers write JavaScript

news.ycombinator.com

61–70 of 342 posts

Re: How true hackers write JavaScript

#62

In most places this code would never pass a code review and would be considered plain unacceptable. I guess I'd prefer some place where this code style is the norm.

Go into academia. You'll receive no code review, and you'll deal with pretty bad code, but you'll be free to write however you want as long as it works and can be published.

"pretty bad code"? That's an euphemism.

Re: How true hackers write JavaScript

#63
post #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

In contrast, hn.js only weighs 2.1kb and contains all the business logic.

Re: How true hackers write JavaScript

#64

Earlier quoted context omitted.

Go into academia. You'll receive no code review, and you'll deal with pretty bad code, but you'll be free to write however you want as long as it works and can be published.

"pretty bad code"? That's an euphemism.

yes

Re: How true hackers write JavaScript

#65
Thanks for sharing this nice bit of code; we always learn from examples, we should see more of this.

I feel the naming is too short, things are just a little too specifically compact and slightly cryptic ... almost like the author is trying to say something ...

I've never said this before about code ... but I think that code is 'smug'!

Like odd facial hair, or one of those valley-specific t-shirts ... the code trying to project how cool it thinks it is!

That code is 'humble-bragging' ...

Re: How true hackers write JavaScript

#66
post #41

function aeach (fn, a) { return Array.prototype.forEach.call(a, fn) } Does this win anything over using forEach directly?

A lot of things like this will work on both arrays (thta have a .forEach method) and things that behave enough like arrays such that Array.prototype.forEach works for them. The downside of this approach (if you subscribe to the ideology of OO) is that if you make something completely unlike an array internally, like a linked list, you can’t use this function on it. Whereas, if you wrote a .forEach method for it, you…

DOM functions like getElementsByClassName return a nodelist, which as you say behaves like an array.

Re: How true hackers write JavaScript

#67
post #4

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

It's also a piece of code that hasn't been rewritten to fit whatever buzz is the buzz today. I wish more of the world was written with code that need not be rewritten every five years.

Re: How true hackers write JavaScript

#69
post #41

function aeach (fn, a) { return Array.prototype.forEach.call(a, fn) } Does this win anything over using forEach directly?

This works with array-like objects which aren't arrays. It's important if you want to support older browsers which don't implement NodeList.forEach(). Well, it's really just for old IE.

If you're targeting modern browsers this is no longer an issue, as they all support NodeList.forEach(). It's worth noting that many DOM APIs return live collections, so depending on what you're doing you might want to clone it first. Luckily now with ES2015 you can use Array.from(list) or just [...list].

Re: How true hackers write JavaScript

#70

    function hidestory (ev, el, id) {
      for (var i=0; i 
That 3 there is the problem with “simple” JS. It’s tightly coupled to the HTML but they live in completely different places.

On a side note this 3 should be at least assigned to a variable with a meaningful name.

Post reply on HN