-
Notifications
You must be signed in to change notification settings - Fork 211
Coding standards
jpmckinney edited this page Sep 13, 2010
·
11 revisions
Follow 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 this:
(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 array. [source]
- Declare all local variables with
var
before they are used. - Remove trailing commas from array and object definitions, i.e.:
var object = { foo: 'bar' }
notvar object = { foo: 'bar', }