Nodejs Study Notes

Source: Internet
Author: User

Profile

Cookies
Replace the pit in the Chung
Array out-of-bounds and undefined size issues
Dom's efficiency Dom Way set element compatibility
The loading order of JS files in HTML
BOM+UTF-8 encoding for Excel files
Post default submit form or page variable
Redis Hmset Hgetall
jquery repeatedly binds the same action without failing
Pop-up sub-page modal
Echarts Setting the Connect, cursor information, repeating legend grid setting page margins, y:-300 setting location not visible
Hash Chart Setup Series.type: "Scatter" data is a single coordinate point
Array.splice () syntax
About Async
Console.time () Console.timeend ()
MongoDB Stream

1. Operation of Cookies

Express provides bodyparser to operate cookies and sessions, but it is more convenient to operate cookies via jquery:

To add a cookies.js reference to HTML:

<script src= "Plugins/jquery.cookie.js" ></script>

In JS, go to cookies:

var predata = $.cookie ("Reportpageinfo");

Set the value of the cookie:

$.cookie ("Reportpageinfo", Json.stringify (Predata), {expires:30});

2. Problems with String.Replace ()

The first parameter in String.Replace () is the object that you want to replace, but the default mechanism for replace replaces it only once, and if there are multiple characters to be replaced in the string to be processed, it is best to use regular expressions and specify a global substitution:

> "2015/12/20". Replace (/\//g, ")

' 20151220 '

3. Array out-of-bounds problem and undefined size

Do not know is not JS continuity, in Nodejs, array subscript out of bounds, will not prompt the error, but return undefined, so if the boundary conditions of the algorithm is not determined, it may produce a series of unintended results.

> l1 = [1, 2, 3, 4]

[1, 2, 3, 4]

> L1[5]

Undefined

Even if the undefined is subscript as an array, there is still no error, but returns undefined. At present, the understanding of JS is not deep, but feel that the grammar is a little less rigorous.

> l2=[]

[]

> l2[l1[66]]

Undefined

Undefined is a special object:

> Undefined > 0

False

> Undefined < 0

False

> undefined = = 0

False

> undefined = = 345

False

> Undefined > 345

False

> Undefined < 345

False

> Undefined > ' erwtsd '

False

> Undefined < ' ERWTSD '

False

> undefined = = ' ERWTSD '

False

> Undefined > "

False

> Undefined < "

False

> undefined = = "

False

Regardless of the type of comparison, undefined returns false in If...else ... structure, with array out of bounds problem, even if the program executes normally, it is difficult to determine that the program is executed according to the expected logic.

May need to combine | | Eliminate undefined interference.

4. Dom Efficiency and compatibility

When you manipulate page elements directly through the DOM, each change in the page element triggers the overall re-rendering of the page, even if the element belongs to the class. Therefore, DOM is inefficient in its approach.

You can do this again by doing all of the things in the DOM object.

On the compatibility, in practice the DOM way to create the elements in chrome can be normal display, Firefox Textnode content cannot be displayed, in the form of jquery set back to normal, do not know if there are other issues.

5. File load order in HTML

HTML files in accordance with the written order of loading, if the first load of JS file needs to use the post-loaded JS file content, you will not find the required content.

HTML is loaded directly into HTML:

<% include./... /.. /include/footer.html%>

6. Excel file Encoding

The content of the Excel file is encoded as Bom+utf-8, and if the characters in the blank Excel file are written with only utf-8 encoding and no BOM, the same garbled character will appear.

BOM: ' \ufeff '

7,setinterval and Clearinterval

After you start the socket listener using polling, if you do not close, the polling will still exist after the method ends, affecting the progress bar judgment, and you need to turn off polling after you have finished using it.

SetInterval (func, time); The method returns a generated polling corresponding ID, which is passed to Clearinterval when polling is closed.

Clearinterval (Socketid);

8. Post default submission form or page variable

When a form exists in an HTML page, the page content (global variable) may be passed as a parameter if the Ajax.post method has no parameters. If the contents of the global variable are too large, the request entity too large error may be caused.

9. Redis operation

Set Redis value in node: Set key value pair form value: Client.hmset (Label, key, Value, callback) Get key value pair value: Client.hgetall (label, callback) The result is the JSON format data, you can directly take the value of the corresponding key 10, jquery repeatedly binding the same action will not fail jquery's click, Focus, change and other methods, the same element is multiple binding, each bound method does not overwrite the previous binding method, Executes continuously after the event is triggered. Duplicate bindings can cause the same operation to be executed multiple times. It's a good idea to make sure you don't expand the impact before each binding, or to unbind it first. 11, sub-page modal temporarily only know how to set and control explicit hiding, the implementation of the principle is not clear. $ ("#modalID"). Modal ("show"); 12, Echarts image linkage settings After the image is drawn, connect the current image object with all other objects via the Connect method, and each object is bound once to achieve the image linkage. If you need to set up a legend to control all images, you need to include all the graphics legend and the corresponding series items in all images, the data of serises item can be empty, hide the unwanted legend, The corresponding graphic can be controlled explicitly by the reserved legend. To set the image cursor information via Option.tooltip.formatter:

Option.tooltip.formatter = function (params) {
var res = params[0].name + ' <br/> ';
Res + = Params[0].seriesname + ":" + params[0].value;
return res;
}

Set the Y property of legend, hide legend

{legend: {

Y:-300,
Data:createconnectedseriesname (obj, data)

}

}

13. Hash Chart Settings

The type of Xaxis and YAxis is "value", the type of all data in the series is "scatter", and each element of the data part is a point with a horizontal ordinate, so it is not necessary to set the data portion of the Xaxis.

14, about Array.splice ()

The splice method can be used to insert an element, delete an element at a specified position, and use:

Arrayobject.splice (index,howmany,item1,....., ItemX)

Index is an array subscript, the second parameter is the number of deleted elements, set to 0 o'clock, inserts the specified content starting with the third argument after the specified element.

15, about the process Control--async

Async.waterfall executes several functions sequentially, the latter can receive the previous callback return value as a parameter, and the callback of the last function returns the return value of the Waterfall method. Async.mapseries (array, iterator, callback) takes the element in the array as an argument, executes the iterator method sequentially, and passes iterator (NULL, item) in the callback method Returns the execution result, and in the final callback, the data part is an array of returned results in order of execution. Async.maplimit (arr, limit, iterator, callback) are similar to mapseries, but can be in parallel with a limit of Async.parallell performs several functions in parallel, but the function must display a definition well, so far it feels less practical and can be replaced by Maplimit. 16, JS code Execution time statistics

Console.time ("Label1");
...
Console.timeend ("Label1");

Counts the code execution time between the two, in milliseconds. Time and Timeend parameters must be one by one corresponding, otherwise the error can not find when to start or terminate.

17, Mongoose.stream

The Find method in mongoose Converts the result to the query object return, which may close the memory cap of the Nodejs engine when the returned result data is large, causing the program to terminate.

The Mongoose.stream method returns a query result one at a time, which is relatively more efficient.

1 varstream =table.find (query, projection). Sort ({2Ct:1,3Duk:14}). Lean (true). Stream ();5Stream.on (' Data ',function(Singlerec) {6 Datas.push (SINGLEREC);7}). On (' Error ',function(err) {8CallBack (Err,NULL);9}). On ("Close",function() {TenConsole.log ("Rec Length:", datas.length); OneCallBack (NULL, datas); A});

Nodejs Study Notes

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.