Node. JS Programming Specification

Source: Internet
Author: User

B.1 Indent

Because the deep function nesting is easy to write in the node. JS code, too many spaces are inconvenient for reading, so we choose two spaces to indent

B.2 Line width

To make sure that it is easy to read on any device, we recommend limiting the line width to 80 characters.

B.3 Statement delimiter

It is recommended to use semicolons (; , even if there is only one statement on a line, do not omit the semicolon.

B.4 variable definition

Always use var to define variables instead of implicitly defining them by assignment. Because a variable implicitly defined by an assignment is always a global variable, namespace pollution is caused.

When defining variables using VAR, make sure that each statement defines a variable, rather than separating multiple variables by commas (,).

B.5 variable name and property name

We use the small hump-type nomenclature (lower camel case) as a naming convention for all variables and attributes, and it is not recommended to use any single-letter variable name.

Varyourname = ' byvoid ';

B.6 function

For general functions We also use the small hump-like nomenclature. But for the constructor name of the object (or not strictly the name of the "class"), we use the large hump-type nomenclature (upper camel case), also known as the Pascal nomenclature

var function () {  return ' something ';}; function anotherfunction () {  return ' anything ';} function datastructure () {  this. Someproperty = ' initialized ';} 
B.7 quotation marks

There is no semantic difference between single quotation marks (') and double quotation marks (") in JavaScript, both of which are available. We recommend that all be unified as single quotes, because JSON and XML stipulate that they must be double quotes, which makes it easy to directly reference without escaping.

B.8 initialization of associative arrays

Put var = {On one line, one pair of key values below each line, keep the indentation of two spaces, end with a semicolon,}; Finally, a separate line is also started. For each pair of key values, no quotation marks are used unless there are spaces or illegal characters in the key name.

B.9 equals

try to use = = = rather than = = to determine equality, because = = contains implicit type conversions

B.10 named functions

Name the constructor and callback function as much as possible so that you can see a clearer call stack when you are debugging.

For callback functions, the node. JS API and the individual third-party modules usually contract the first parameter of the callback function to err, and if no error occurs, its value is undefined.

b.11 Object Definition

Try to define all the member functions through the prototype, define the attributes within the constructor, and then create the object with the new keyword on the constructor. Never define a property as a prototype, because attributes in different instances point to the same address when the property being defined is an object . Unless it is necessary to avoid defining member functions inside the constructor, there is a runtime closure overhead.

//Error:functionFooobj (bar) { This. Bar =Bar;  This. Func =function() {Console.log ( This. arr); };} FooObj.prototype.arr= [1, 2, 3];varObj1 = newfooobj (' obj1 '));varObj2 = newfooobj (' obj2 ')); Obj1.arr.push (4); Obj1.func (); //[1, 2, 3, 4]Obj2.func ();//[1, 2, 3, 4]
b.12 inheritance

First, avoid the use of complex inheritance, such as multiple inheritance or deep inheritance trees. If you do need to inherit, try to use the inherits function provided in the Util module of node. js.

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.