Coding standards for JS files
General considerations
All functions must be terminated with a semicolon (;) as well as a closing brace.
WRONG:
Drupal.behaviors.tableSelect = function (context) {
$('form table[th.select-all]:not(.tableSelect-processed)', context).each(Drupal.tableSelect);
}CORRECT:
Drupal.behaviors.tableSelect = function (context) {
$('form table[th.select-all]:not(.tableSelect-processed)', context).each(Drupal.tableSelect);
};Proposals
Start variable names that contain a jQuery object with dollar sign. Example:
WRONG:
element = $('#some-id');
$element = document.getElementById('some-id');CORRECT:
$element = $('#some-id');
element = document.getElementById('some-id');
Thanks for that. A few
Thanks for that. A few thoughts:
Incorrect convention
The convention for terminating functions with a semi-colon is incorrect. A semi-colon is only placed after the end of an expression. A function is not considered an expression when written without code on its lefthand side, whereas the code example given above correctly has a semi-colon.
Therefore,
function () {} alert("foo");orfunction foo() {} alert("bar");are both correct and don't need a semi colon. No errors will occur if code is placed on the right hand side (on the same line) in this case.no error with function bar(){} alert("foo") but...
Yes, There is no error if your code is:
function foo(){} alert("bar");but it will raise an error Expected ";" with the following code:
foo = function(){} alert("bar");The one in the convention.
Yes, that's exactly what I
I'm afraid you misunderstood what I wrote. Your latter example is considered an expression since it's preceeded by
foo =, therefore it needs a semi colon. But, setting the convention that evenfunction foo() {} alert("bar");needs an additional semi-colon makes no sense. It's like saying thatif (foo) {}should also be suffixed by such. Sure, you could, but why? An arbitrary convention is not sane just because it doesn't cause a parse error.The interpretation of
function foo(){};would be "A declaration of the function foo, followed by an empty statement" since there already is an implicit end-of-statement after function declarations that aren't expressions.