Show HN: The JavaScript Way, a new book for learning modern JS
github.com
github.com
const getTweets = (username) => return fetch(`https://api.twitter.com/${username}`)
getTweets('nevon').then(console.log).catch(console.error)
Really isn't that bad, although you have to know promises. With regular callbacks, I guess you have to know that functions can be passed as arguments, but you'll have to learn that quite soon regardless. const getTweets = (username, callback) => {
const req = new XMLHttpRequest()
req.onload = function () {
return request.status === 200 ? callback(undefined, JSON.parse(this.responseBody)) : callback(Error(request.statusText))
}
req.onerror = callback
req.open('get', `https://api.twitter.com/${username}`, true)
req.send()
}
getTweets('nevon', (err, tweets) => {
if (err) {
console.error(err)
} else {
console.log(tweets)
}
})
Most of the confusing crap in there is because of XMLHttpRequest, not asynchronicity.With async functions, which I personally am not a huge fan of, you can get synchronous looking code if you believe that's more beginner friendly:
async function getTweets (username) => {
const response = await fetch(url)
return response.json()
}
getTweets('nevon').then(console.log).catch(console.error)But of all the languages not to pick, I'd content that JavaScript is one of the very worst. (PHP and anything Visual Basic are honorable mentions.)
80% of everything is crap.
The weird edge cases in JavaScript are easily avoided when teaching a subset of es2016 to new programmers.
The basics: variables, loops, functions, etc... are all fine. It's got pretty good functional programming support, too, for beginners.
It's not a "purity" point of view, it's an "ease of use for the purpose" point of view. JavaScript does weird things that stymie experienced devs - a newbie hitting them will be completely lost. What's worse, as I said, is that it does these things because it's poorly designed/its design doesn't adequately cover all the edge cases it should, so newbies will learn the wrong information about why things do or don't happen.
The edge cases are not easily avoided because you don't know what a beginner will or won't try to do when working on their own. That means that not only might they do an okay thing in most languages that doesn't work in JS, but that they may also do something BAD that DOES work in JS, because JS is weird. For instance, the way JS handles variable hoisting and scoping is bonkers compared to most other languages - not stuff you can avoid while learning to program, and making it easy to learn the wrong practice as a result.
"80% of everything is crap" is a good thing to know when doing development work, but it isn't helpful when learning. The goal of learning in this context is mastery of the basics of development; 80% won't be good enough.
This is true regardless of which language you use to start with. Computing in general is so ridiculously over-complex and constructed entirely of layered kludges, that beginners are going to trip and fall into lots of these crevasses no matter what, unfortunately.
The first lesson advantage of being able to start the coding immediately (everyone has a browser) is not to be dismissed. If you try to do this in Python - otherwise an excellent choice - you will spend the first ~day of the course installing Python and explaining about command lines, editors and the REPL.
This seems trivial to us, but it's a huge hurdle for many first time programmers. The JS version of this is "go to this URL, then right click and select Inspect. Start typing in the console."
JavaScript has been ruined by professional programmers (mostly Rubyists) who never learned how to use the object model, or how to structure functional code well. So we get ES6 classes and promises and async and all of this junk that makes the language impossible to build and debug.
And I say this as a full time JavaScript programmer of ~5 years, after ~5 years of Ruby. I love both classic JavaScript and Ruby, but ES6 is an abomination.
node -v
v0.12.7I love having these three choices and only these three choices. I think class inheritance, promises/async, and other similar features add complexity for little gain.
I used to revel in using fancy features. I was so excited when I found a real need for a DSL in one of my Ruby applications. I built it. I felt like a rock star. Then 12 months later I realized I could have wrote the same code, more understandably, with functions and a few arrays to index data.
You refer to JS core as "poor" and "bad" which are not very descriptive words. I am sure you have material criticisms but what are they? I read every JavaScript critique I see, and so far they mostly boil down to "it's not C" or "it's not Ruby" or "it's not Haskell".
Yah, it's not. It's simpler and unless you are building a compiler or a video game it's often better because of the simplicity.
The problem with JavaScript, IMO, is that people come from other languages, don't actually learn how the JS primitives are best used, and prematurely try to extend the language. See Promises, see classes, see async. These are all features that were only added because the developers couldn't be bothered to learn how to use exactly the features you mention.
You can argue this as bad usability on the part of JS, but the reason it gained so much traction is not just because it was in Netscape....
These are actually amazingly powerful feature implemented in a small space. As a beginner they are incredibly usable. It's only hard for experts coming from C or Ruby, who are the people wrecking the language with extensions. Lispers seem to get it.
> `Object.create`
:(
Understanding prototypal inheritance seems essential to me, and Object.create() is the cleanest way to show it in action.
The book also presents the alternative class syntax and numerous ES2015 additions like block-scoped variables and arrow functions, to name a few.
How would you teach OOP basics to a JS newcomer then? Using only class without mentioning prototypes?
https://www.amazon.com/Front-End-Web-Development-Ranch-Guide...