node. JS Global Object

Source: Internet
Author: User
Tags setinterval

node. JS Global Object

In browser JavaScript, the window is usually a global object, while the global object in node. JS is global, and all global variables (except the global itself) are properties of the global object.

In node. js, we can directly access the properties of global without having to include it in the app.

Global objects and global variables

The fundamental role of global is to be the host of variables. As defined by ECMAScript, variables that meet the following conditions are global variables:

    • Variables defined at the outermost layer;
    • The properties of the global object;
    • Implicitly-defined variables (variables that are not directly assigned) are defined.

When you define a global variable, the variable also becomes a property of the global object, and vice versa. It is important to note that in node. js You cannot define variables at the outermost layer because all user code belongs to the current module, and the module itself is not the outermost context.

Note: always use var to define variables to avoid introducing global variables, because global variables pollute namespaces and increase the risk of code coupling.

__filename

__filename represents the file name of the script that is currently executing. It will output the absolute path to the location of the file, and the file name specified with the command-line arguments is not necessarily the same. If the value returned in the module is the path to the module file.

Instance

To create the file main.js, the code looks like this:

Output global variable __filename value console.log (__filename);

  

Execute the main.js file, as shown in the following code:

$ node main.  JS/Web/com/runoob/nodejs/main.  JS             
__dirname

__dirname represents the directory where the current execution script resides.

Instance

To create the file main.js, the code looks like this:

Output global variable __dirname value console.log (__dirname);

  

Execute the main.js file, as shown in the following code:

$ node main.  JS/Web/com/runoob/nodejs         
SetTimeout (CB, MS)

The setTimeout (CB, MS) global function executes the specified function (CB) After the specified number of milliseconds (ms). : SetTimeout () executes the specified function only once.

Returns a handle value representing the timer.

Instance

To create the file main.js, the code looks like this:

function Printhello () {   console.log ("Hello, world!");} After two seconds, perform the above function settimeout (Printhello, 2000);

  

Execute the main.js file, as shown in the following code:

$ node main.  JSHello,world!     
Cleartimeout (t)

The cleartimeout (t) global function is used to stop a timer that was previously created by SetTimeout (). The parameter T is a timer created by the SetTimeout () function.

Instance

To create the file main.js, the code looks like this:

function Printhello () {   console.log ("Hello, world!");} After two seconds, execute the above function var t = setTimeout (Printhello, 2000);//Clear Timer cleartimeout (t);

  

Execute the main.js file, as shown in the following code:

$ node main.  JS 
SetInterval (CB, MS)

The setinterval (CB, MS) global function executes the specified function (CB) After the specified number of milliseconds (ms).

Returns a handle value representing the timer. You can use the clearinterval (t) function to clear the timer.

The SetInterval () method keeps calling functions until Clearinterval () is called or the window is closed.

Instance

To create the file main.js, the code looks like this:

function Printhello () {   console.log ("Hello, world!");} After two seconds, perform the above function SetInterval (Printhello, 2000);

  

Execute the main.js file, as shown in the following code:

$ node main.  JS 

Hello, world!. Hello, world!. Hello, world!. Hello, world!. Hello, world!. ......

The above program will output "Hello, world!" every two seconds, and will be executed permanently until you press CTRL + C button.

Console

Console is used to provide console standard output, which is a debugging tool provided by the JScript engine of Internet Explorer, and later becomes a browser implementation standard.

node. JS uses this standard to provide a console object that is consistent with customary behavior to output characters to the standard output stream (STDOUT) or standard error stream (stderr).

Console method

The following are the methods of the console object:

Console.log (): Prints characters to the standard output stream and ends with line breaks.

Console.log receives several parameters, and if there is only one argument, it outputs the string form of the parameter. If there are multiple parameters, it is output in a format similar to the C-language printf () command.

The first argument is a string, and if there are no arguments, only one line break is printed.

Console.log (' Hello world '); Console.log (' Byvoid%diovyb '); Console.log (' Byvoid%diovyb ', 1991);

  

The result of the operation is:

Hello World byvoid%   
    • Console.error (): Same as Console.log () usage, just output to standard error stream.
    • Console.trace (): Outputs the current call stack to the standard error stream.
Console.  Trace();  

The result of the operation is:

Trace:at object.<anonymous> (/home/byvoid/consoletrace.js:1:71) at Module._compile (module.js:441:26) at Object .. JS (module.js:459:10) at Module.load (module.js:348:31) at Function._load (Module.js:308:12) at array.0 (module.js : 479:10) at Eventemitter._tickcallback (node.js:192:40)

  

Instance

To create the file main.js, the code looks like this:

Console.info ("program Begins execution:"), var counter = 10;console.log ("Count:%d", counter); Console.time ("Get Data");////execute some code// Console.timeend (' Get Data '); Console.info ("The program finishes executing. ")

  

Execute the main.js file, as shown in the following code:

$ node main.  JS program starts execution:  count:  get data:0ms  program execution complete

Process

Process is a global variable, which is a property of the global object.

It is used to describe the object of the current node. JS process State and provides a simple interface to the operating system. Usually when you write a local command-line program, you have to deal with it. Some of the most common member methods of the Process object are described below.

Instance

To create the file main.js, the code looks like this:

Process.on (' Exit ', function (code) {  //) The following code will never execute  setTimeout (function () {    console.log ("The code will not execute");  } , 0);    Console.log (' Exit code: ', Code);}); Console.log ("End of program execution");

  

Execute the main.js file, as shown in the following code:

$ node main.  JS program execution end exit code:0    
Process Property

Process provides a number of useful properties that allow us to better control the interaction of the system:

Instance

To create the file main.js, the code looks like this:

Output to Terminal process.stdout.write ("Hello world!" + "\ n");//Read through Parameters Process.argv.forEach (function (val, index, array) {   Console.log (index + ': ' + val);}); /Get Execution path Console.log (process.execpath);//Platform Information Console.log (process.platform);

  

Execute the main.js file, as shown in the following code:

$ node Main.jshello world!0:node1:/web/www/node/main.js/usr/local/node/0.10.36/bin/nodedarwin

  


Instance

To create the file main.js, the code looks like this:

Output current directory Console.log (' current directory: ' + PROCESS.CWD ()),//Output current version Console.log (' current version: ' + process.version);//Output Memory usage console.log ( Process.memoryusage ());

  

Execute the main.js file, as shown in the following code:

$ node Main.js current directory:/web/com/runoob/nodejs current version: V0.10.36{rss:12541952, heaptotal:4083456, heapused:2157056}

  

Excerpt from: http://www.runoob.com/nodejs/nodejs-global-object.html

node. JS Global Object

Related Article

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.