Global variables and global objects in node

Source: Internet
Author: User

Global objects and global variables

Concept: All properties can be accessed anywhere in the program, that is, global variables. In JavaScript, typically window is a global object, and node. JS is global, and all global variables are properties of the global object, such as console, process, and so on.
The fundamental function of global is to host the world variable, satisfying the condition called the global variable
1. Variables defined at the outermost layer
2. Properties of Global objects
3. Implicitly-defined variables
▲ in node it is not possible to define variables at the outermost layer, because all user code belongs to the current module, and the module itself is not in the outermost context.

Global Object Process

It is used to describe the object of the current node. JS process State, providing a simple interface to the operating system, usually written 本地命令行 by a program that will use it.
Here are a few common ways to do this:
1. PROCESS.ARGV: An array of command-line arguments, used on the command line, the first element is node, the second element is to run the script file name, starting from the third element each element is a run parameter.
We are building a file called process.js , which only needs to contain the code to print this function information can be

console.log(process.argv);

Then run the file from the command line,

For example, this method is where all the arguments become an array
2. Process.stdout (stdout) standard output stream, the bottom of the console.log we use is to use this implementation
The following code is the implementation console.log code:

console.log=function(msg){    process.stdout.write(msg);}console.log("shangyilong");
    1. Process.stdin is the standard input stream, in the old version of the standard stream, when initially he was paused by default, want to read data from the standard input stream, must pass process.stdin.resume() the recovery stream and write the flow of the event corresponding function, In the new version, there is no recovery flow operation, which means that the standard input stream is started by default, and of course the new standard stream operation is compatible with the old version of the stream operation.
      This is an old version of the standard flow operation
//先要恢复流process.stdin.resume();//绑定上相应的事件process.stdin.on(‘data‘,function(some){    process.stdout.write(some.toString());});

The following new version of the standard input stream operation

//the corresponding event on the binding, In addition to the data event, readable can also listen for input, and the callback function for this event has no parameters  Process.stdin.on (, Span class= "hljs-function" >function   ()  {     var  data = Process.stdin.read (); if  (Data!== null )    {process.stdout.write (data); }}); //process.stdin.on (' readable ')  process.stdin.on ( End ' , function   ()  { process.stdout.write ();});  
    1. Process.stderr is the standard error stream, and process.stdout almost just the color of the output is not the same, see

      This is the effect of Webstrom in the command line is not visible
    2. PROCESS.CWD (): Represents the absolute path of the current file, this and __dirname is a different __dirname output is through node execution of the JS file is located in the absolute path, see it can understand,
    3. Process.on ()

      • Process.on (' Exit ', function () {}) indicates that it will be triggered when the program exits.
process.on(‘exit‘,function(){    console.log("process will exit");})//output  process will exit
    • Process.on ("SIGINT", function () {}) (Sigint=>signal interrupted) will trigger this function when we exit the program, see the following code:
Process.stdin.on (' readable ', function(){    vardata = Process.stdin.read ();if(Data!==NULL) {process.stdout.write (data); }});//Process.stdin.on (' readable ')Process.stdin.on (' End ', function(){Process.stdout.write (' is end ');}); Process.on (' exit ', function(){Console.log ("process would exit");}); Process.on (' SIGINT ', () =>{Console.log (' program'll exit ');//Indicates exiting the programProcess.exit ();});

Is the result graph after exiting:

7. Process.nexttick (). To understand this you also need to know the 事件循环 mechanism in node, if you do not know can read my other blog post:
node, you have to know something.
The function is to set a task for the event loop, and node invokes the callback function for the next event loop response.
This function can also be counted as the complexity of the task of splitting, programming small process; Let's take a look at the code for a specific explanation:

 function dosomething(data){Console.log (data);} Console.time ("One"); function do1(ARG,CB){DoSomething (ARG);    DoSomething (ARG);    DoSomething (ARG);    DoSomething (ARG);    DoSomething (ARG);    DoSomething (ARG); CB ();} Console.timeend (' One '); Console.time (' both '); function do2(ARG,CB){DoSomething (ARG);    DoSomething (ARG);    DoSomething (ARG);    DoSomething (ARG);    DoSomething (ARG);    DoSomething (ARG); Process.nexttick (CB);} Console.timeend (' both ');//output:1ms//output:0ms

According to the time test above, we can see the efficiency.

For more information, please see the next article.

This is a series of node, you can see in this column other about node article, will be updated, there are questions please leave a message below

Global variables and global objects in node

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.