"Writing maintainable JavaScript" reading notes (top)

Source: Internet
Author: User

Recently read the book "Write Maintainable JavaScript," in order to deepen memory, simply make a note, but also let not read the students have a general understanding.

First, the programming style

The program is written for people to read, so the programming style of a team should be consistent.

1, Indentation: One is the use of tab indentation, one is the use of whitespace indentation, each has pros and cons, any choice, to maintain consistency. Individual prefers tab indent.

2, the end of the statement need to add a semicolon, to avoid unnecessary bugs.

3, naming: First of all to semantic, using the Hump-type nomenclature, small hump that is the first letter lowercase, followed by the first letter of each word capitalized; The big Hump is the first letter capital, then the same small hump; the variable name prefix should be a noun (myName), the function name prefix should be a verb (getName), Constants are all capitalized (my_name), very intuitive, and constructors use the large hump nomenclature (DateFormat).

4, note: single-line comment with//, multiline comment with/**/, single line comment//There is always a space after, the next line of code indentation level should be consistent with the note. It should be annotated before it is difficult to understand or easy to think of as the wrong code.

5, curly brace alignment recommended this:

if (  ) {}else{    }

This is not recommended:

if (  ) {}else{    }

6. Avoid using the WITH statement.

7, the For in statement is used to traverse the object's property values, and to iterate through the array, use the For loop

8, define the variable at the top of the function, the variable must be written on the Var statement, the definition of multiple variables can be separated by commas, such as:

var name = "Wangmeijian",    =%,    = "Boy";

At the same time, strings uniformly use double quotes.

9. When judging whether two values are equal, use = = = or!==, it is not recommended to use = = and! = because JavaScript has a forced type conversion mechanism that can cause unexpected results.

10. In JavaScript, eval () is not the only function that can execute a JavaScript string, or it can be done using the function constructor, SetTimeout () and SetInterval (). For example:

var wmj=New Function ("alert (' Hi ')" "document.body.style.background= ' Red '",  "Document.Write (' It's Now" + (new// Book This example has errors, this article has been corrected

These three kinds of usage are relatively rare.

11, the original packaging type: First look at a section of code

var name = "Wangmeijian"; Console.log (Name.touppercase ());

A string is not a method, but you can still use a method such as toUpperCase (), because the JavaScript engine behind this statement creates a new instance of type string, which is destroyed immediately after. String is the original wrapper type of the string.

"Writing maintainable JavaScript" reading notes (top)

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.