This page shows no content on kindle webbrowser.
Writing Eloquent JavaScript Without CoffeeScript
21–30 of 55 posts
Re: Writing Eloquent JavaScript Without CoffeeScript
#22He's got to be trolling. Multiline ternary operators aren't in my book better than an if block. He 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.
Using strict mode will avoid these kinds of mistakes.
Re: Writing Eloquent JavaScript Without CoffeeScript
#23Re: Writing Eloquent JavaScript Without CoffeeScript
#24 x == 1
? alert(x)
:(
alert('Error'),
x = 1,
alert('All Fixed!')
);Re: Writing Eloquent JavaScript Without CoffeeScript
#25It 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.
Totally -- I think that 0 (which is a perfectly valid value for a number to be) bites people doing lazy initialization all the time. 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? i…
Re: Writing Eloquent JavaScript Without CoffeeScript
#26I don't want to sound like an asshole and I'm saying nothing about the author, but in my personal experience I found lining up variables or =s was something I worried about when I first started programming. Now my mind's eye just totally skips variable declaration when visually code parsing, variable declaration is meaningless drivel that exists only to stop typos (a good purpose). I had an opinion on the layout of t…
Also with Javascript's function-scoped variables and the fact it will let you declare them more than once in a given scope (the second and subsequent decleration being taken as either a NOOP or a basic assignemtn (if there is assignment with the decleration), declaring variables at the point of first use can be dangerous as it is easy, especially if you are messing around in global scope (which is to be availaded generally, but can't always be), to create hard to diagnose bugs due to variable values being clobbered because the same name is used elsewhere.
Re: Writing Eloquent JavaScript Without CoffeeScript
#27This page shows no content on kindle webbrowser.
Re: Writing Eloquent JavaScript Without CoffeeScript
#28Using 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.
Not only is it more readable, it's more correct in the example given.
"if" is a statement. The ternary operator (?:) is an expression, that returns a value. The semantics are different, and using the ternary operator "merely" to replace an if statement, especially where no assignment of the ternary operator's return value is made, is a code smell.
The ternary operator is great if you're replacing a simple "if something assignment to X else other assignment to X." Any other use is questionable.