Created
April 7, 2013 15:44
-
-
Save alloyking/5330983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Declarations with var: Always | |
var | |
Constants: | |
Use NAMES_LIKE_THIS for constant values. | |
Never use the const keyword as it not supported in Internet Explorer. | |
If a value is intended to be constant and immutable, it should be given a name in CONSTANT_VALUE_CASE. ALL_CAPS additionally implies @const (that the value is not overwritable | |
ex: | |
TIMEOUT_IN_MILLISECONDS = 60; | |
Functions inside IF's (aka blocks) | |
NO | |
if (x) { | |
function foo() {} | |
} | |
YES | |
if (x) { | |
var foo = function() {} | |
} | |
The ability to create closures is perhaps the most useful and often overlooked feature of JS. Here is a good description of how closures work . http://jibbering.com/faq/faq_notes/closures.html | |
One thing to keep in mind, however, is that a closure keeps a pointer to its enclosing scope. As a result, attaching a closure to a DOM element can create a circular reference and thus, a memory leak. For example, in the following code: | |
function foo(element, a, b) { | |
element.onclick = function() { /* uses a and b */ }; | |
} | |
the function closure keeps a reference to element, a, and b even if it never uses element. Since element also keeps a reference to the closure, we have a cycle that won't be cleaned up by garbage collection. In these situations, the code can be structured as follows: | |
function foo(element, a, b) { | |
element.onclick = bar(a, b); | |
} | |
function bar(a, b) { | |
return function() { /* uses a and b */ } | |
} | |
Always use normal for loops when using arrays. | |
function printArray(arr) { | |
var l = arr.length; | |
for (var i = 0; i < l; i++) { | |
print(arr[i]); | |
} | |
} | |
JQUERY | |
Good examples | |
// Good | |
if ( condition ) { | |
// expressions | |
} | |
// Good | |
while ( condition ) { | |
// expressions | |
} | |
// Good | |
var i = 0; | |
for ( ; i < 100; i++ ) { | |
// expressions | |
} | |
// Good | |
var prop; | |
for ( prop in object ) { | |
// expressions | |
} | |
// Good | |
if ( condition ) { | |
// expressions | |
} else { | |
// expressions | |
} | |
// Good | |
if ( condition ) { | |
// expressions | |
} else if ( condition ) { | |
// expressions | |
} else { | |
// expressions | |
} | |
// Good | |
try { | |
// expressions | |
} catch ( e ) { | |
// expressions | |
} | |
// Good | |
try { | |
// expressions | |
} catch ( e ) { | |
// expressions | |
} finally { | |
// expressions | |
} | |
// Good | |
object[ array[ 0 ] ]; | |
obj/array | |
var object = {}, | |
array = []; | |
Function calls | |
Always include white space | |
foo( arg ); | |
foo( "string", object ); | |
foo( options, callback ); | |
foo( node, "property", 2 ); | |
Assignments should always have a semicolon after them. | |
Semicolons should always be followed by a newline. | |
Assignments in a declaration should always be on their own line. Declarations that don't have an assignment should be listed together at the start of the declaration. For example: | |
// Bad | |
var foo = true; | |
var bar = false; | |
var a; | |
var b; | |
var c; | |
// Good | |
var a, b, c, | |
foo = true, | |
bar = false; | |
Strict equality checks (===) should be used in favor of ==. The only exception is when checking for undefined and null by way of null. | |
// Check for both undefined and null values, for some important reason. | |
undefOrNull == null; | |
Type Checks | |
String: typeof object === "string" | |
Number: typeof object === "number" | |
Boolean: typeof object === "boolean" | |
Object: typeof object === "object" | |
Plain Object: jQuery.isPlainObject( 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 | |
jQuery uses double quotes: | |
var double = "I am wrapped in double quotes"; | |
Strings that require inner quoting must use double outside and single inside. | |
var html = "<div id='my-id'></div>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment