Javascript Best Practices
javascripttoolbox.com
javascripttoolbox.com
WHAT?
No, you do NOT mix JavaScript into your HTML elements. Total disregard for separation of concerns, maintainability nightmares abound.
You put script, surprisingly, into the <script> tag.
<a href="/posts/1244/comments/new" onclick="Comment.new(this); return false;">New Comment</a>2. Violates separation of concerns
3. Complicates maintainability
4. Events are bound directly to elements rather than bubbling and being managed via delegation.
onclick="return vote(this)"[edit] my point was to not take this site as an example of best-practices...
I have found that when you're looking for JS help or references, you should use the most recent available.
http://google-styleguide.googlecode.com/svn/trunk/javascript...
My understanding of Javascript has increased; I now think of Javascript as a proper language to code in and not just a collection of statements to make HTML elements move/change-color. Crockford videos are a must see!
The only issue might be that there is a lot of repetition of concepts between videos and sometimes it gets boring, but it is always worth it.
<script>
The type isn't necessary anymore. We know it's JS.I use it when it's useful, but if the name of the property should not be determined at run-time, I always do the dot notation. I've never heard of anyone taking issue with it, either.
The most common problem here is people using the dot notation for properties like "object.default" or "object.class" that are syntactically invalid.
The rest of the article is a strange mix of good advice and some cases of pretty-bad code being presented as better than very-bad code.
This article would more accurately be titled best practices for DOM interactions since most of the suggestions deal with DOM issues rather than pure language issues (and most of these are easily dealt with through the use of a good library like jquery).
The Google Javascript style guide someone mentioned above is much more relevant to pure language issues, and is the one style guide I reference frequently (or should reference frequently).
When another programmer asks me to fix bugs in some code, I get them to make it parse JSHint first. Most of the time, fixing errors to make it conform either fixes the bug or helps them understand the code enough to fix it themselves.
> GOOD: document.forms["formname"].elements["inputname"]
> BAD: document.formname.inputname
Square bracket notation is awesome for mixing in variables - it saves you from using the evil eval() - and obviously BAD is worse than GOOD, but if you have no variables what about this:
> ?: document.forms.formname.inputname
If there's no functional difference it's shorter and simpler than GOOD
I personally find it more useful to deal with forms using some kind of abstraction on top so I doubt I'd use either variations in anything other than a very simple page.
I use the elements collection because the behaviour of adding each input as a property to the parent form element is a mistake.
I use the bracket notation because it highlights the difference between the implementation symbols and the actual data which is being manipulated. What I mean is that I see a property specified with the dot notation on the same level as an identifier and as such its name is just to remind us humans of its purpose, whereas a property accessed using bracket notation is important both to us and to the program since it specifies a data point.
And no, JavaScript is not a "non-typed language." AFAIK JavaScript is both dynamically and weakly typed. Being in the latter category does mean that it will implicitly convert values to other types in certain scenarios.
I don't understand why he would recommend unary as a replacement for casting. It obfuscates the intent of your code, which is especially concerning since this reads like a guide for beginners.
parseFloat("08") === parseFloat("09") === 0
http://stackoverflow.com/questions/850341I recommend the 7.0 release candidate:
http://downloads.activestate.com/Komodo/releases/
More info:
http://www.activestate.com/komodo-ide http://www.activestate.com/komodo-edit
[1]: Original version: http://code.google.com/p/js2-mode/
[2]: Improved (IMO) fork: https://github.com/mooz/js2-mode
Additionally, you can configure JS[HL]int to use flymake[3]. The trick is to improve performance by having it talk to a running JavaScript process (e.g. a node server) instead of starting a new one each time.