// anonymous function as event handler$el.on('click',function(){returntrue;});// optional name to aid debugging with stack traces$el.on('click',functionelClick(){returntrue;});// anonymous function as methodMyObject.prototype.isTrue=function(){returntrue;};// immediately-invoked function expression (IIFE)(function(){console.log('Welcome to the Internet. Please follow me.');})();
Function declarations:
// use a named declaration when appropriate mainly// for internal helper logic and organizationfunctionnamedFunction(){returntrue;}
Never declare a function in a non-function block (if, while, etc). Assign the function to a variable instead. Browsers will allow you to do it, but they all interpret it differently, which is bad news bears.
Always use var to declare variables. Not doing so will result in global variables. We want to avoid polluting the global namespace. Captain Planet warned us of that.
Use one var declaration per variable.
It's easier to add new variable declarations this way, and you never have
to worry about swapping out a ; for a , or introducing punctuation-only
diffs.
// badvaritems=getItems(),goSportsTeam=true,dragonball='z';// bad// (compare to above, and try to spot the mistake)varitems=getItems(),goSportsTeam=true;dragonball='z';// goodvaritems=getItems();vargoSportsTeam=true;vardragonball='z';
Declare unassigned variables last. This is helpful when later on you might need to assign a variable depending on one of the previous assigned variables. Multiple unassigned
variables can be declared in a single-line var declaration.
Variable declarations get hoisted to the top of their scope, but their assignment does not.
// we know this wouldn't work (assuming there// is no notDefined global variable)functionexample(){console.log(notDefined);// => throws a ReferenceError}// creating a variable declaration after you// reference the variable will work due to// variable hoisting. Note: the assignment// value of `true` is not hoisted.functionexample(){console.log(declaredButNotAssigned);// => undefinedvardeclaredButNotAssigned=true;}// The interpreter is hoisting the variable// declaration to the top of the scope,// which means our example could be rewritten as:functionexample(){vardeclaredButNotAssigned;console.log(declaredButNotAssigned);// => undefineddeclaredButNotAssigned=true;}
Anonymous function expressions hoist their variable name, but not the function assignment.
functionexample(){console.log(anonymous);// => undefinedanonymous();// => TypeError anonymous is not a functionvaranonymous=function(){console.log('anonymous function expression');};}
Named function expressions hoist the variable name, not the function name or the function body.
functionexample(){console.log(named);// => undefinednamed();// => TypeError named is not a functionsuperPower();// => ReferenceError superPower is not definedvarnamed=functionsuperPower(){console.log('Flying');};}// the same is true when the function name// is the same as the variable name.functionexample(){console.log(named);// => undefinednamed();// => TypeError named is not a functionvarnamed=functionnamed(){console.log('named');};}
Function declarations hoist their name and the function body.
Conditional statements such as the if statement evaluate their expression using coercion with the ToBoolean abstract method and always follow these simple rules:
Objects evaluate to true
Undefined evaluates to false
Null evaluates to false
Booleans evaluate to the value of the boolean
Numbers evaluate to false if +0, -0, or NaN, otherwise true
Strings evaluate to false if an empty string '', otherwise true
if([0]){// true// An array is an object, objects evaluate to true}
// badif(test)returnfalse;// acceptable (only for very simple statements and early function return)if(test)returnfalse;// goodif(test){returnfalse;}// badfunction(){returnfalse;}// goodfunction(){returnfalse;}
If you're using multi-line blocks with if and else, put else on the same line as your
if block's closing brace.
Use /** ... */ for multi-line comments. Include a description, specify types and values for all parameters and return values.
// bad// make() returns a new element// based on the passed in tag name//// @param {String} tag// @return {Element} elementfunctionmake(tag){// ...stuff...returnelement;}// good/** * make() returns a new element * based on the passed in tag name * * @param {String} tag * @return {Element} element */functionmake(tag){// ...stuff...returnelement;}
Use // for single line comments. Place single line comments on a newline above the subject of the comment. Put an empty line before the comment.
请发表评论