13. Node. JS Global Object

Source: Internet
Author: User
Tags parse error posix setinterval stack trace

Mainly used for debugging, displaying information, focusing on examples
In browser JavaScript, the window is usually a global object, and the global object in node. JS is


# # #__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.

# # #__dirname
__dirname represents the directory where the current execution script resides.

# # #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.

# # #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.


# # #setInterval (CB, MS)
The SetInterval () method keeps calling functions until Clearinterval () is called or the window is closed.


##################################################################
Console
Console is used to provide the standard output for consoles
Console method
1
Console.log ([data][, ...])
Prints characters to the standard output stream and ends with a newline character. The method receives several parameters and, if there is only one argument, 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.
2
Console.info ([data][, ...])
The purpose of this command is to return an informational message, which differs greatly from the Console.log, except that only the text will be printed in chrome, and the rest will display a blue exclamation point.
3
Console.error ([data][, ...])
The output error message. The console displays a red fork when an error occurs.
4
Console.warn ([data][, ...])
The output warning message. The console appears with a yellow exclamation point.
5
Console.dir (obj[, Options])
Used to examine an object (inspect) and display it in a format that is easy to read and print.
6
Console.time (label)
The output time, which indicates that the timing starts.
7
Console.timeend (label)
End time, indicating that the timing is over.
8
Console.trace (message[, ...])
Outputs the current call stack to the standard error stream. The current execution of the code in the stack of call path, this test function is very helpful, as long as you want to test the function inside the console.trace to join the line.
9
Console.assert (value[, message][, ...])
Used to determine if an expression or variable is true, receives two arguments, the first argument is an expression, and the second argument is a string. The second parameter is output only if the first argument is false, otherwise there will be no result.

############### #例子

Console.info (' program start execution '); var counter = ten; Console.log (' Count:%d ', counter); Console.time (' Get data '= 10 * (1.132** 5); Console.log ('%f ', Final_money); Console.timeend (' get Data '); Console.info (' program execution complete ') ; Execution Result: program starts execution count:1018.587977335224310. 190ms Program Execution Complete

##################################################################
Process
Process is a global variable, Object
######################################## #事件
1
Exit
Triggers when the process is ready to exit.
2
Beforeexit
Triggers this event when node clears the event loop and there are no other arrangements. In general, node exits when no process is scheduled, but the ' beforeexit ' listener can be called asynchronously so that node continues execution.
3
Uncaughtexception
Triggers this event when an exception bubbles back to the event loop. If you add a monitor to an exception, the default action (print stack trace information and exit) does not occur.
4
Signal event
Triggers when the process receives a signal. The list of signals is described in the standard POSIX signal names, such as SIGINT, SIGUSR1, etc.

#事件实例

function (code) {    //  The code below does not perform    setTimeout (function  () {        Console.log ( ' This code will not execute ');    },0);    Console.log (' exit code: ', Code);}); Console.log (' End of program' 0

####
Exit Status Code
Status code
Name & Description
1
Uncaught Fatal Exception
There are uncaught exceptions and are not handled by the domain or uncaughtexception processing function.
2
Unused
Keep
3
Internal JavaScript Parse Error
JavaScript source causes parsing errors when the Node process is started. is very rare and will only be available when Node is being developed.
4
Internal JavaScript Evaluation Failure
The JavaScript source starts the Node process, and the return function fails when evaluated. is very rare and will only be available when Node is being developed.
5
Fatal Error
Fatal unrecoverable error in the V8. Typically prints to stderr with the following: FATAL ERROR
6
Non-function Internal Exception Handler
The exception is not caught and the inner exception handler is not known why it is set to on-function and cannot be called.
7
Internal Exception Handler run-time Failure
An uncaught exception, and the exception handler handled itself with an exception thrown. For example, if Process.on (' uncaughtexception ') or Domain.on (' error ') throws an exception.
8
Unused
Keep
9
Invalid Argument
may be given an unknown parameter, or the given parameter has no value.
10
Internal JavaScript Run-time Failure
JavaScript's source code throws an error when it starts the node process, which is very rare and will only be available when node is being developed.
12
Invalid Debug Argument
The parameter--debug and/or--DEBUG-BRK are set, but the wrong port is selected.
128
Signal Exits
If Node receives a fatal signal, such as Sigkill or SIGHUP, then the exit code is 128 plus the signal code. This is the standard Unix practice and the exit signal code is placed at a high level.


########################################### #Process Properties
1
StdOut
Standard output stream.
2
StdErr
Standard error stream.
3
Stdin
Standard input stream.
4
Argv
The Argv property returns an array of arguments that are made when the command line executes the script. Its first member is always node, the second member is the script file name, and the remaining members are the parameters of the script files.
5
Execpath
Returns the absolute path of the Node binary file that executes the current script.
6
execargv
Returns an array of command-line arguments between the node executable and the script file when the command line executes the script.
7
Env
Returns an object that is a member of the environment variable for the current shell
8
ExitCode
When the process exits the code, the exit code does not need to be specified if the process is optimized to exit by Process.exit ().
9
Version
Node's version, such as v0.10.18.
10
Versions
A property that contains the version and dependencies of node.
11
Config
An object that contains the JavaScript configuration options that are used to compile the current node execution file. It is the same as the "Config.gypi" file that is generated by running the./configure script.
12
Pid
The process number of the current process.
13
Title
The process name, the default value is "Node", and you can customize the value.
14
Arch
Current CPU architecture: ' Arm ', ' ia32 ' or ' x64 '.
15
Platform
Run the program on the platform system ' Darwin ', ' FreeBSD ', ' Linux ', ' SunOS ' or ' Win32 '
16
Mainmodule
The alternative method of Require.main. At different points, if the main module changes at run time, Require.main may continue to return to the old module. It can be thought that the two refer to the same module.

# #属性实例

// output to Terminal process.stdout.write (' love ' + ' \ n '); // read Process.argv.forEach by parameter (function  (value, index, array) {    + ': ' + value '); }); // gets the path of the execution Console.log (process.execpath); // Platform Information Console.log (process.platform); execution result:Love 0:D: \program Files\nodejs\node.exe1: F:\nodejs _code\9_global_object\4_process_properties.jsd:\program Files\nodejs\node.exewin32

########################################### #Process method
1
Abort ()
This causes node to trigger the Abort event. Causes node to exit and generate a core file.
2
ChDir (directory)
Changes the directory of the current worker process and throws an exception if the operation fails.
3
CWD ()
Returns the working directory of the current process
4
Exit ([code])
Ends the process with the specified code. If omitted, code 0 will be used.
5
Getgid ()
Gets the group identity of the process (see Getgid (2)). Gets the numeric ID of the group, not the name.
Note: This function is only available on POSIX platforms (for example, non-Windows and Android).
6
Setgid (ID)
Set the group identifier for the process (see Setgid (2)). You can receive a numeric ID or group name. If a group name is specified, blocking waits resolves to a numeric ID.
Note: This function is only available on POSIX platforms (for example, non-Windows and Android).
7
Getuid ()
Gets the user ID of the process (see Getuid (2)). This is the user ID of the number, not the user name.
Note: This function is only available on POSIX platforms (for example, non-Windows and Android).
8
Setuid (ID)
Set the user ID of the process (see Setuid (2)). Receives a numeric ID or string name. The group name is specified, blocking the wait resolution to the numeric ID.
Note: This function is only available on POSIX platforms (for example, non-Windows and Android).
9
GetGroups ()
Returns an array of group IDs for the process. POSIX systems are not guaranteed to be, but node. JS is guaranteed to have.
Note: This function is only available on POSIX platforms (for example, non-Windows and Android).
10
Setgroups (groups)
Sets the group ID of the process. This is an authorization operation, so you need to have root privileges, or have cap_setgid capability.
Note: This function is only available on POSIX platforms (for example, non-Windows and Android).
11
Initgroups (user, Extra_group)
Read the/etc/group and initialize the group access list, using all the groups where the member is located. This is an authorization operation, so you need to have root privileges, or have cap_setgid capability.
Note: This function is only available on POSIX platforms (for example, non-Windows and Android).
12
Kill (pid[, signal])
Sends a signal to the process. The PID is the process ID, and signal is the string description of the signal being sent. The signal name is a string, such as ' SIGINT ' or ' SIGHUP '. If omitted, the signal will be ' SIGTERM '.
13
Memoryusage ()
Returns an object that describes the memory state used by the Node process in bytes.
14
Nexttick (callback)
Once the current event loop ends, the call goes back to the function.
15
Umask ([mask])
Sets or reads the mask for the process file. The child process inherits the mask from the parent process. If the mask parameter is valid, the old mask is returned. Otherwise, the current mask is returned.
16
Uptime ()
Returns the number of seconds that Node has been running.
17
Hrtime ()
Returns the high resolution time of the current process, in the form of an array of [seconds, nanoseconds]. It is an arbitrary event relative to the past. This value is independent of the date and is therefore not affected by clock drift. The main purpose is to measure the performance of the program through precise time intervals.
You can pass the previous results to the current process.hrtime (), which returns the time difference between the two, used to benchmark and measure the interval.


# # #方法例子

// 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 ()); execution Result: current directory: F:\nodejs_code\9_global_object current version: V8. 11.221446656,  7159808,  4316816,  8224}

13. Node. JS Global Object

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.