-
Notifications
You must be signed in to change notification settings - Fork 211
Coding standards
evolvingweb edited this page Sep 13, 2010
·
11 revisions
Follow Drupal’s JavaScript coding standards
Also:
- I prefer single-quotes to double-quotes.
- I prefer the jQuery short-hand
$(function() { ... });
to$(document).ready(function() { ... });
.
If you want to use jQuery’s $
variable, wrap your code like so:
(function($) {
// code using $ variable
})(jQuery);
If you want to use global variables within the scope of your code, wrap your code like this:
(function() {
var foo = 'bar'; // yay, it's almost like I'm global
})();
If you want to use global variables in the global scope, put the variable in the AjaxSolr namespace:
AjaxSolr.foo = 'bar';
- Use an object if you need an associative array, not an object. [source]
- Declare all local variables with
var
before they are used. - Remove trailing commas from array and object definitions.
This is bad:
var array = [ 'foo', 'bar', ]
var object = { foo: 'bar', }
This is good:
var array = [ 'foo', 'bar' ]
var object = { foo: 'bar' }