This first article will talk about some of the programming details of Nodejs.
1, traversing the array
For (var i=0, l=arr.length; i<l; i++)
One of the benefits of this is that it takes less than one step per loop to get the length of an array object, and the longer the array, the more obvious the value.
2, determine the true and false variables
if (a) {...}//a= ', a= ' 0 ', a=[], a={}
If conditions determine the results are: false, True, true, true. This result is different from the PHP result and don't confuse it. It is also necessary to distinguish between it and a situation that is similar to a non identity judgment.
3, 0 value not identical judgment
1 if (0 = = ' 0 ') {...}//true
2 if (0 = = []) {...}//true
3 if (0 = [0]) {...}//true
4 if (0 = {}) {...}//f Alse
5 if (0 = null) {...}//false
6 if (0 = = undefined) {...}//false
In fact, there are many of these bizarre judgments, I only listed the more common. If you want to understand the rules, see my other blog post: "JavaScript" delves into JavaScript's relational operations and if statements.
4, the parseint Trap
var n = parseint (s); s= ' 010 '
The statement executes with an n value of 8 instead of 10. Although a lot of people know this, but the programming will inevitably make mistakes, I have a deep understanding. So, it's best to write in the following way, without making a mistake.
var n = parseint (s, 10);
5, the variable must be declared before use
Although it's not an error to use a variable directly without declaring it, it's easy to make mistakes. Because the interpreter interprets it as a global variable, it is easy to make an error with the name of another global variable. Therefore, must develop the variable to use before must declare the good habit first.
6, asynchronous situation in the loop
For (var i=0, l=arr.length; i<l; i++) {
var sql = ' select * from Nx_user ';
Db.query (SQL, function () {
Sys.log (i + ': ' + sql);
});//db.query for table query operation, asynchronous Operation
}
You will find that the output is the same, and is the output when i=arr.length-1. Because JavaScript is single-threaded, it executes the asynchronous operation of the entire loop after it has finished synchronizing the contents. The anonymous callback function in code is an asynchronous callback. The For loop and some subsequent synchronizations have been executed when the function is executed. For the closure principle, the function retains the contents of the SQL and I variables for the last loop of the For loop, which results in the error.
What about it? There are two solutions, one of which is to use the immediate function, as follows:
For (var i=0, l=arr.length; i<l; i++) {
var sql = ' select * from Nx_user ';
(function (sql, i) {
db.query (SQL, function () {
Sys.log (i + ': ' + sql);
});//db.query for table query operations, asynchronous Operations
}) ( SQL, i);
Another way is to extract the asynchronous operations section and write a single function, as follows:
var outputsql = function (sql, i) {
db.query (SQL, function () {
Sys.log (i + ': ' + SQL ';
});//db.query for table query operations, is an asynchronous operation
}
for (Var i=0, l=arr.length i<l; i++) {
var sql = ' select * from Nx_user ';
Outputsql (sql, I);
}
7, in the large number of data processing, as far as possible to avoid circular nesting.
Because the cycle nesting processing time will increase with the amount of data exponentially number of levels, so should try to avoid. In this case, if there is no better way, the general strategy is to change time in space, that is, to create a two-level cyclic data hash map. Of course, the specific situation and specific analysis. Another thing to say is that some of the methods themselves are a loop body, such as Array.Sort (the method should be implemented with a two-tier loop) and need to be noted when used.
8, try to avoid recursive calls.
The advantage of recursive invocation is that the code is simple and easy to implement, and its shortcomings are important, as described below:
(1) The size of the function stack will grow linearly with the recursion level, and the function stack has the upper limit value, and when the recursion reaches a certain layer, the function stack will overflow, which causes the program error;
(2) Each recursive layer will add additional stack and stack operations, that is, the function of the process of saving the site and restore the scene.
Therefore, the recursive invocation should be avoided as far as possible.
9, the scope of the module file isolation.
When the JavaScript module file is compiled, node has wrapped the contents of it, as follows:
(function (exports, require, module, __filename, __dirname) {
Your JavaScript file code
});
This allows for scope isolation between each module file. So, when you write Nodejs module files, you don't need to add a layer of scope isolation encapsulation. As in the following code format, only one additional layer of function calls is added, which is not recommended:
(function () {
...
}) ();
10, arrays and objects do not mix
The following is an example of an error code:
var o = [];
o[' name ' = ' liming ';
The mix of arrays and objects can cause unpredictable errors. One of my colleagues had a very strange problem, read the code first:
var o = [];
o[' name ' = ' liming ';
var s = json.stringify (o);
He thought the Name property of Object o would be in the JSON string, and the result would be no. At that time I was also very strange, but I have a hunch that the array and object mixed with the problem, try it, it is really the problem. I later found in the ECMA specification that the array was in the Ja Rule when serializing. So, to develop a good programming habits, the correct use of arrays and objects, do not mix.
11, Promise Elegant programming
It is believed that people who have contacted Nodejs have had this experience, and when asynchronous callbacks are nested in asynchronous callbacks, the code becomes confusing and lacks readability. Nodejs's dilemma can be overcome by promise. Promise is like a carving device that makes your code elegant and beautiful. Promise has a + specification, there are several ways to implement online, you can refer to.