Order
Go on to the top two, this is the end of the article.
Blocks
? There is the code of {}, we wrap the line processing.
Bad
if (test)
return false;
Good
if (test) return false;
Good
if (test) {
return false;
}
Bad
function () {return false;}
Good
function () {
return false;
}
Comments
? Use/** for multiline annotations ... * *. Contains descriptive information, parameter types, and return values.
Bad
Make () returns a new element
Based on the passed in tag name
//
@param Tag
@return Element
function make (tag) {
... stuff ...
return element;
}
Good
/**
* Make () returns a new element
* Based on the passed in tag name
*
* @param Tag
* @return Element
*/
function make (tag) {
... stuff ...
return element;
}
? Use//For single-line comments. Single-line comments are placed on a single new line. Place a blank line before the comment.
Bad
var active = true; Is current tab
Good
Is current tab
var active = true;
Bad
function GetType () {
Console.log (' fetching type ... ');
Set the default type to ' no type '
var type = This._type ' no type ';
return type;
}
Good
function GetType () {
Console.log (' fetching type ... ');
Set the default type to ' no type '
var type = This._type ' no type ';
return type;
}
? For some questions, add fixme or Todo before the comment, which will quickly help developers understand the intent of the code quickly.
? Use//fixme: annotation Issue
function Calculator () {
Fixme:shouldn ' t use a global here
Total = 0;
return this;
}
? Use//TODO: annotation Solution for problem
function Calculator () {
Todo:total should be configurable by a options param
this.total = 0;
return this;
}
Type Casting & Coercion
? Performs a mandatory type conversion before the declaration.
? String
=> This.reviewscore = 9;
Bad
var totalscore = This.reviewscore + ';
Good
var totalscore = ' + This.reviewscore;
Bad
var totalscore = ' + This.reviewscore + ' total score ';
Good
var totalscore = This.reviewscore + ' total score ';
? For numeric conversions, use parseint, with the cardinality of the type conversion.
? If parseint becomes your bottleneck, in performance reasons, you need to use the "displacement" operation. Then write a note explaining why you did it.
var inputvalue = ' 4 ';
Bad
var val = new number (inputvalue);
Bad
var val = +inputvalue;
Bad
var val = inputvalue >> 0;
Bad
var val = parseint (Inputvalue);
Good
var val = number (inputvalue);
Good
var val = parseint (Inputvalue, 10);
Good
/**
* parseint makes my code slow.
* To increase speed, use the displacement operation to force the string to be converted to a number.
*/
var val = inputvalue >> 0;
? Boolean
var age = 0;
Bad
var hasage = new Boolean (age);
Good
var hasage = Boolean (age); This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130710/39007.html
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.