For example, safe object navigation. This code in Spider/CoffeeScript:
var x = a?.b?.c?.d;
compiles to something like: var x = typeof a !== "undefined" && a && a.b && a.b.c ? a.b.c.d : void 0; var x = _.getPath(a, "b.c.d");
There are definitely advantages to having this pattern baked into the language, though.I'm mighty tempted to write a Webpack plugin to make `?` usable in vanilla JS.
Even if you don't know CS very well, I dare you to take a look at the JavaScript this compiles to and call it more readable:
(req, res)->
getUserIds(req.query)
.then (ids)->
Promise.all ids.map (id)->
getUser(id).then (user)-> user.friends.count
.then (response)-> res.send 200, response
.catch (error)-> res.send 500, errorYou code compiles to:
(function(req, res) {
return getUserIds(req.query).then(function(ids) {
return Promise.all(ids.map(function(id) {
return getUser(id).then(function(user) {
return user.friends.count;
});
}));
}).then(function(response) {
return res.send(200, response);
})["catch"](function(error) {
return res.send(500, error);
});
});
Not what intended, I assume :)Edit: But that aside, to be fair, I'll readily agree that it is easy to mess up CoffeeScript's significant whitespace if you don't pay attention to it. For example, you add a multi-line callback to one of those single-line callbacks at your peril!
But the tradeoff is, no mismatched braces/parenteses. Personally, I find the warts are not that hard to avoid, and the overall impact on my productivity is positive.
More readable? Eh...
I can grok either at about the same speed.
I mean, if you're talking about a deep understanding of what the code does, I agree with you. But when you're writing a lot of code that basically looks like that, it's useful to be able to take it in at a glance.
With that said, on wider level, what's the allure of these JavaScript replacement languages? Is JavaScript syntax really that difficult for some developers to wrap their head around? I can understand something like Dart that has an underlying goal of performance, but the purposes of things like this or CoffeeScript genuinely confuse me.