function aeach (fn, a) { return Array.prototype.forEach.call(a, fn) }
Does this win anything over using forEach directly?How true hackers write JavaScript
41–50 of 342 posts
Re: How true hackers write JavaScript
#42Re: How true hackers write JavaScript
#43That 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) : []
In fact, using the online babel transpiler, it transpiles the es6 version you provided precisely into hn's version:
To use es6 to achieve what this tiny piece of javascript could already do, you probably need to introduce some tremendous dependency like babel just to be compatible with all kinds of weird browsers out there. What does es6 give here? Not very much in my opinion.
Re: How true hackers write JavaScript
#44That might be one of the most readable pieces of code that I've ever read.
Re: How true hackers write JavaScript
#45That might be one of the most readable pieces of code that I've ever read.
Re: How true hackers write JavaScript
#46Earlier quoted context omitted.
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
#47function aeach (fn, a) { return Array.prototype.forEach.call(a, fn) } Does this win anything over using forEach directly?
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 could use it interchangeably with arrays anywhere the code calls .forEach.
Without trying it, I’ll guess that this is used on the arguments magic parameter. It’s notorious for being array-like but not an array.
Re: How true hackers write JavaScript
#48Earlier quoted context omitted.
Really? Plenty of needlessly abbreviated function names IMO. At a quick glance is it immediately obvious what posf or arem are without looking into the context in which they're being used? Don't get me wrong it's a nice piece of code but super-readable it isn't.
Well, I disagree. It is abbreviated, but since the whole file is self-contained and the definitions are put at the beginning, there is enough information to infer the function's semantics without being excessively specific. I think well defined organization of abstractions is an often overlooked but surprisingly effective way to achieve readability
You mean you can just read the function definition to see what it does?
Re: How true hackers write JavaScript
#49https://twitter.com/triskweline/status/798443082740023296 > Valve's Steam Store renders on the server, uses ancient jQuery 1.8, loads 12 unminified JavaScripts. > It moved 3.5 billion dollars in 2015.
There are real metrics from top industry leaders[1][2] to prove that loading time has a clear impact on conversion rates.
>Consider this when you need 5000 npm modules and a team of 10 to compile your startup's login form.
Unminified JavaScripts files and bloated JS are both wrong, once again: not sure of what's the point of comparing both.
[1] https://medium.com/@vikigreen/impact-of-slow-page-load-time-... [2] http://glinden.blogspot.com/2006/11/marissa-mayer-at-web-20....
Re: How true hackers write JavaScript
#50function aeach (fn, a) { return Array.prototype.forEach.call(a, fn) } Does this win anything over using forEach directly?