| CARVIEW |
Getting Started
API Reference
Plugins
Support
About jQuery
Views
Personal tools
JQuery Core Style Guidelines
From jQuery JavaScript Library
Contents |
Spacing
Use tabs to indent your code.
Be sure to use liberal spacing in your code.
// No:
if(blah==="foo"){
foo("bar","baz",{zoo:1});
}
// Yes:
if ( blah === "foo" ) {
foo( "bar", "baz", {zoo: 1} );
}
Equality
Strict equality checks (===) should be used in favor of == wherever possible.
Blocks
Braces should always be used on blocks.
// NO:
if ( true )
blah();
// YES:
if ( true ) {
blah();
}
Don't put statements on the same line as a conditional.
// NO:
if ( true ) return;
if ( true ) blah();
// YES:
if ( true ) {
return;
}
if ( true ) {
blah();
}
Don't use ternary operators instead of if/else statements.
Don't use object && object.method() instead of if/else statements (except in conditionals).
Type Checks
- String:
typeof object === "string" - Number:
typeof object === "number" - Boolean:
typeof object === "boolean" - Object:
typeof object === "object" - Function:
jQuery.isFunction(object) - Array:
jQuery.isArray(object) - Element:
object.nodeType - null:
object === null - null or undefined:
object == null - undefined:
- Global Variables:
typeof variable === "undefined" - Local Variables:
variable === undefined - Properties:
object.prop === undefined
- Global Variables:
RegExp
All RegExp operations should be done using .test() and .exec(). "string".match() is no longer used.
Nodes
.nodeName should always be used in favor of .tagName.
.nodeType should be used to determine the classification of a node (not .nodeName).
Don't attach expandos to nodes - only attach data using .data().
© 2010 The jQuery Project
Sponsored by Media Temple and others.
