JavaScript Things I Wish I Knew Much Earlier In My Career
smashingmagazine.com
smashingmagazine.com
"One thing that amazed me is how much easier my life got once I read up thoroughly on the math and string functions of JavaScript. You can use these to avoid a lot of looping and conditions."
As a colleague and friend of mine, I can tell you he's trying to be an educator and help other people become better coders. If that means he's aimed some stuff under a line for "good" that's because he's trying to help people get over that line.
But, smashingMagazine puts out some awesome content (albeit mostly link rolls, but well illustrated), I have a hard time criticizing them.
Completeness is not the same as 'bestness'.
Doug's book tells you what you should know, Flannigan's book tells you what you can use.
"The good parts" is opinion and commentary. It's not fact.
I want hard facts about what the language permits. I can make my own mind up about what is good and what is bad thanks.
"The good parts" is an interesting read once you know the language, but I certainly wouldn't recommend it as a first book on js.
You might be the best programmer on HN for all I know, but personally I'm willing to admit Doug's decades of programming and international reputation might mean he can teach me something, without me having to learn it the painful way.
I don't. I hate naming things as patterns with a passion FWIW. I'm not really sure you can really learn good taste from a book :/ All you need is the language reference, and years of practice...
Really a good article, especially for a developer like myself who doesn't do all that much JavaScript.
http://jmesnil.net/weblog/wp-content/uploads/2009/09/RzRcw.j...
There are a lot of gems in this article though (aside from that).
This can be mitigated with parenthesis around the conditional statement.
"ternary notation ... [is] great for saving (a little) time"
I like it because it allows for immutable variables (enforced by fiat, of course), without writing four lines of "if ... else .."
That being said, it can be abused: http://www.google.com/codesearch?hl=en&lr=&q=%22%3D%...
We start with your "readable" approach:
var a;
if (condition(x)) {
a = 4;
}
else {
a = 5;
}
That has all the glory of line noise, repetition, and a declaration to make our language happy. This example is a little exaggerated. We can make our declaration more useful and our condition more of an exception: var a = 5;
if (condition(x)) {
a = 4;
}
That is still two assignments or, rather, the "a =" pattern repeated. And that is just for a trivial single branch. How can we abstract that out? You have two options. For simple conditions, we can do this: var a = condition(x) ?
4 :
5 ;
It might be better to abstract it out like this: var a = value_for_a_with_environment(x);
Sure, you may be using a ternary operator or if...else underneath the hood in value_for_a_with_environment(), but give that function a relevant name, and someone can get the gist without having to know the details.It could just be arrangement and syntax we despise. Here is another language's approach to the ternary operator:
(setf a (if (condition x)
4
5))
Or with consideration given to the environment: (setf a (value-for-a-with-environment x))
Once I learned some of the value and abstractions of functional programming, I stopped feeling the ternary operator's use was ugly and realized it was more that I hated the line noise involved in using it. That colon looks a lot like a semicolon.Also, it is limited. In nontrivial code, I have seen cascades of ternary operators.
var a = cond1(x) ?
4 :
cond2(x) ?
5 :
cond3(x) ?
6 :
7 ;
or var a = cond1(x) ? 4
: cond2(x) ? 5
: cond3(x) ? 6
: 7 ;
Pick your favorite arrangement.We really want a non-binary-looking structure like this:
(setf a (cond (cond1 4)
(cond2 8)
(cond3 12)
(t nil)))I'm pretty sure that other automatic refactoring tools can do the same.
However, I have noticed that I tend to use them liberally, and have since reverted back to if-else when there is something more than "simple" going on in the condition or post-condition.
Math.max.apply(Math,[3,5,1,7,9]);
I was always confused at why mouseover / out events bubble in the insane way they do, now I am glad for the behaviour (I just wish enter / leave came sooner)
(YAML died on fuzziness in the specification mistakenly perceived as virtue. "YAML's not dead!" Compared to JSON, it is, and thank goodness.)
Incidentally, if you aren't using it strictly, you're losing some of the advantages yourself. If your parser accepts single-quoted strings, or even worse if your serializer generates them (ick!), just as one example, you're in some trouble, even if it all works for you right now.
Edit: Apart from the two already mentioned in this thread. (Javascript the definitive guide and javascript the good parts)
For JS: JavaScript: The Definitive Guide, 5th Edition by Flanagan. Until Secrets of the JS Ninja (or the Rhino book's 6th edition) comes out, then get those.
Out of curiosity, what have you worked with previously?
It's just recently that I started working on a real project (C#) and it's been fun so far and I'm eager to finish it and get onto the next thing.
Thank you for the sources!
I highly recommend taking the scripts in the tutorial apart and testing to see what happens. No matter how much theoretical reading or tutorials I did, nothing came close to actual hands-on coding.
Best of luck in your project!
One sentence explaining that JS doesn't have variable scope proceeded by a sentence explaining that it actually does have scope? JS absolutely does have scoping rules. The follow up to this sentence (the section labeled "Anonymous Functions And The Module Pattern") seems to be ignorant of the presence of prototypes (in a prototype-based language!) and uses a needlessly complex and confusing way of explaining how to expose functions. What's wrong with the prototype?
What's wrong with the prototype?
Nothing too bad, I suppose.
Making things private is a little bit cleaner when you use the anonymous function/module method. By default, none of the methods are visible unless you explicitly expose them, whereas with a normal Javascript object you end up jumping through hoops to make private class methods actually be private - those hoops pretty much match what you do with the module pattern anyways, so...
More importantly, the "needlessly complex and confusing" way of exposing functions (I don't necessarily disagree that it departs from "write what you mean", but I don't know that it's either too complex or confusing, it's a pretty simple pattern and often results in shorter code than prototyping would, esp. with private functions and data) is used just about everywhere when you work with other people's Javascript libraries. So it's a good thing to be familiar with it, as you're going to be looking at it almost every time you open up the source for any modern JS library.
myarray.sort(function() {return 0.5 - Math.random()})
This just makes every comparison return a random value, which may or may not produce particularly random results, depending on the sort algorithm. It's exactly the bug that bit Microsoft in their not-very-random browser shuffle screen.The thing I wish the JavaScript community knew much earlier in their careers was actual software engineering knowledge, such as Object-oriented, or at least typed, languages.
var car = new Object(); car.colour = 'red'; car.wheels = 4; car.hubcaps = 'spinning'; car.age = 4;
I second the recommendation of JavaScript: The Good Parts - it's a great book :)
And don't forget the `hasOwnProperty` test when iterating over the keys.
That line makes me think the author doesn't know what an associate array is.
var a : Array = []; a['property'] = 'value';
, then I'm setting a property of the object, not an element in the array. It works just as well with
var b : Object = {}; b['property'] = 'value';
The author has clearly not had to use contentEditable :)
He doesn't say how long ago in the past.