Writing Eloquent JavaScript Without CoffeeScript
11–20 of 55 posts
Re: Writing Eloquent JavaScript Without CoffeeScript
#12The first point he raises is a valid argument; CoffeeScript's intro does uglify the JS a little to make its point. But it doesn't take much.
> ...merely inaccurate due to a lack of knowledge on JavaScript...
Kinda think the opposite idea was how CoffeeScript came into being, amirite? It was written to make JavaScript look AND act better. It wasn't developed by people that had no idea how to use JavaScript, and it certainly isn't used exclusively by people who don't know how to use JavaScript, too.
Re: Writing Eloquent JavaScript Without CoffeeScript
#13 > The examples on the CoffeeScript site are purposely written
> to be ugly to make CoffeeScript look even prettier.
... not at all. The side-by-side examples on CoffeeScript.org are showing you the compiled output. The point is to demonstrate -- although it's certainly far from perfect -- how CoffeeScript features can be compiled into clean JavaScript. Really, our goal is to make the JS look as nice as possible.Re: Writing Eloquent JavaScript Without CoffeeScript
#14I'm not sure how many times this will have to be clarified, but the JS code on the CoffeeScript site are not examples, but the compiled JS. Even so, it's in many cases prettier than Oscar Godson's "eloquent" JS, IMHO.
Re: Writing Eloquent JavaScript Without CoffeeScript
#15It should be pointed out that "var a = a || someDefault;" can bite you when 0, "", or null are valid values for a. The empty string in particular can be surprising here at times.
For this reason, CoffeeScript provides the existential operator, which asks: does this variable exist? (Is it not null or undefined?) You can use it in the same places you would use "||", as well as some others:
# In place of `||`
result = answer ? default
# Does `object.value` exist?
if object.value?
# Lazy initialize the speed of the car:
car.speed ?= 60
# Give me the value of `length`, only if `object` exists.
length = jsonResponse.list.object?.lengthRe: Writing Eloquent JavaScript Without CoffeeScript
#16He also completely misses the point about javascript's global-by-default variables and how easy it is to introduce them with typos, properly building classes, etc.
Re: Writing Eloquent JavaScript Without CoffeeScript
#17So it's not just me, then. IMO, writing `someThing ||= {}` is much more eloquent than `var someThing = someThing || {}`. The first point he raises is a valid argument; CoffeeScript's intro does uglify the JS a little to make its point. But it doesn't take much. > ...merely inaccurate due to a lack of knowledge on JavaScript... Kinda think the opposite idea was how CoffeeScript came into being, amirite? It was written…
Re: Writing Eloquent JavaScript Without CoffeeScript
#18Using the ternary operator is more "eloquent" than conditionals? That's true iff "eloquent" means terse in terms of number of characters, but I find if(...){} much more readable.
The ternery operator has some readability problems, but it is an expression, not a statement. Turning statements into expressions is powerful, which is why CoffeeScript's if/then compiles into an expression. Too bad this wan't mentioned.
Likewise, the comma operator allows JavaScript programmers to use a block of consecutive statements as an expression, something the author points out when explaining how to use ternaries in place of if statements.
The notion that you can use a sequence of statements as an expression without having to resort to a "let" form (function () { ...; ...; ...; return foo; })() is another underused construct that can make for improved JavaScript.
Put the two ideas together, and yes you can express a multi-line if statement with shorter keywords, but more importantly you have an if expression without having to wrap it in a function and encumber it with return statements.
I'm sorry that idea wasn't given more attention. If you aren't using JavaScript directly, this pattern may be useful in a more functional, expression-oriented style of programming.
Re: Writing Eloquent JavaScript Without CoffeeScript
#19This page shows no content on kindle webbrowser.
It took my browser 20 seconds to render the text. I've no idea why he would load static content dynamically.
Re: Writing Eloquent JavaScript Without CoffeeScript
#20It should be pointed out that "var a = a || someDefault;" can bite you when 0, "", or null are valid values for a. The empty string in particular can be surprising here at times.