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 () {...});.

Wrap $ and global variables

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';

Avoid Internet Explorer errors

  • 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' } not var object = { foo: 'bar', }
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.