JavaScript coding Style

Source: Internet
Author: User
Tags naming convention

Recently looking at the front-end Daniel Nicbolas C.zakas, "Write maintainable JavaScript code" book. Feel that a lot of knowledge points are written very well, so, write a blog post, summed up it! Coding specifications are important for programming, because if the coding style is inconsistent, the code looks messy and difficult to maintain. Of course, different development teams have different coding specifications, notably, the Google Code specification, the jquery code specification, the Dojo Code specification, and Yahoo! Coding specifications, and so on.

First, indentation Levels ( Indent in )。 Currently the more popular is 2 space indentation and 4 space indentation, such as Google is 2 space, but jquery and Yahoo! is 4 spaces. Instead, Nicbola recommends using the 4-Space tab indent for the following reasons: 1. Most editors default to 4 spaces of 2. In addition, the indentation of two spaces is not obvious; regardless of which method of indentation, team development must use the same indentation, avoid the same project inside, Some use a two-space indent, some with a 4-space indent. second, line length and Line Breaking (line length and line wrapping), many developers like to write a line of code long and long, to drag the horizontal scroll bar can see the entire line of code, in fact, this is not good practice, in a lot of coding norms inside, the number of the president has been limited.In general, the length of a line of code does not exceed 80 characters。 When the letter exceeds the length of the limit (usually 80 letters), be sure to wrap it. The line break needs to be indented, and it's also a two-level indent (so-called two-level indentation is indentation and indentation, which means pressing two tab keys). For example, the following code:
// recommended notation, breaking lines at the end of the operator (the comma is also the operator), and the indentation is correct, using a two-level indent true, 123,        //  is not recommended, because only one level of indentation is true, 123,    //  bad: Not recommended, not at the end of the operator to break the true, 123        , navigator);

to be aware of a break,to prevent the JavaScript engine from automatically adding semicolons at the end of the error, it often breaks at the end of the operator;This is also true in block-level statements, where the following is a break in the IF statement:
if (Isleapyear && isfebruary && Day = = $ && itsyourbirthday &&        noplans) {    Waitanotherfouryears ();}

One exception is that when assigning a variable, the starting position of the second line is aligned with the first character in the right part of the first line's equal sign, which is a bit of a mouthful, just an example, as shown here:
var result = something + anotherthing + yetanotherthing + somethingelse +             anothersomethingelse;

We assign a value to the variable result, and the second row of Anothersomethingelse is aligned to the first character on the right of the first line (that is, the first character of something); third. naming (naming rules)In General, it is recommended to use lowercase letters that begin with the hump naming rules, as follows
var schoolname;

The name of a variable is best preceded by a noun, as follows:
var schoolname;

 

The function name is best to start with a verb, usually with can, have, is, get, set, as follows:
function Getschoolname () {}

Variable type it is best to guess the type of the variable, such as: count, length, size, etc. can know is the number type, and Schoolname, title, color, type and so on is the character string type, in addition, the loop number of commonly used I, J, K, etc.;but there is a class of functions that are recommended to start with capital letters. Construtor (constructor). Constructors are used to create objects, usually using Pascal's naming convention, which is named after the first uppercase hump, so that it can be separated from the normal function as follows:
// recommended Wording function person (name) {    this. Name=function() {    alert (this  . name);}; var New Person ("Nicholas");

  Iv. String (string)JavaScript strings can be wrapped in single quotes or double quotes. The following are allowed:
var name = "Nicholas says, \" Hi.\ ""var name = ' Nicholas says, ' Hi ';  

But for unification, don't sometimes use double quotes, sometimes single quotes.as the following code is not good:
var= "Tom"= "Jane"

There is a problem with the use of strings, try not to use multiline strings (multi-line string), but should be split them into multiple strings, and then with "+" for stitching, as follows:
// no recommended wording var longstring = "Here's the story, the A man named Brady."  // recommended notation var longstring = "Here's the story, of a man" +                 "named Brady.";

The author suggests using double quotes, because many other languages are in double quotes. For example, if you write JavaScript code and write Java code, if you always use double quotes, then you do not have to always think "I am writing JavaScript or java it"! V, statement (block-level statement)Block-level statements mainly include the following: if for while Do...while try...catch...finally and so on, many times, when block-level statements have only one statement, many people like to omit curly braces, as follows:
if (condition) dosomething ();

Now this code is no problem, if after some time, we want to extend the code for this block-level statement, for example, add more than one statement in the IF, as follows:
if (condition) dosomething ();d osomethingelse ();

At this time, Dosomethingelse (); it will be executed anyway, because he is not in the IF, but this is not what we want. Therefore, in order to avoid the expansion of similar problems, should be formed whenever you want to add curly braces, even if there is only one statement, the above example recommended wording, should be as follows:
if (condition) {    dosomething ();}

In addition, the position of the left curly brace ("{") of the block-level element is generally suggested to be written in thebehind the block-level statement start line, Google JavaScript Style Guides, and jquery are recommended for this, as follows:
if (condition) {    else  {    dosomethingelse ();}
Instead of writing at the beginning of another line, the following:
if (condition) {    dosomething ();} Else {    dosomethingelse ();}

And there is,to add a space in front of the left curly brace,As shown below
if (condition) {    dosomething ();}
Six, when a function is called, there is no space between the letter name and the opening parenthesis
// recommended Wording dosomething (item);
// It's not recommended, it looks like a block-level statement. dosomething (item);

seventh, equation (equality)In general, it is recommended to use "= = =" and "When comparing two different types of values for equality! = = "instead of using" = = "and"! = ". Because, if you compare two different types of numeric values, "= =" and "! =" can have coercion type conversions, so the comparison results are sometimes less accurate. For example, a comparison of numbers with strings automatically converts a string into a number:
// The number 5 and string 5 // true // The number and hexadecimal string // true
    


JavaScript coding Style

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.