“This article is participating in the technical topic essay node.js advanced road, click to see details”

Pass parameters to the Node program

Normally we can execute our files using Node xxx.js. In some cases we want to pass some parameters to node. We can add parameters to the file name that need to be passed.

node xxx.js age=18 haha
Copy the code

We can pass it in the programprocessthisBuilt-in objectsGets the passed argument.

console.log(process);
Copy the code

By printing the process built-in object, we see that it contains a lot of information.

The argument vector (argv) property is an argument vectorAn array ofContains the parameters we passed in. The first element is the node installation path, the second argument is the path to the file we are in, and the third and fourth arguments are the ones we pass in.

We get the parameters passed by using the argv array subscript, and we can iterate over the parameters of the array.

console.log(process.argv[2]);
console.log(process.argv[3]);

process.argv.forEach(item= > {
  console.log(item);
})
Copy the code

The output of the node

The Console object is a global object.

  • Console. log: the most common output method
  • Console. clear: Clears the console
  • Console. trace: Prints the call stack for the function. You can tell which function is called.
console.log(process.argv[2]);
console.log(process.argv[3]);
console.clear()

process.argv.forEach(item= > {
  console.log(item);
})
Copy the code

We can see that the current console prints the result of traversal of the argv array, and the previous prints of process.argv[2] and process.argv[3] have been cleaned out.

We can print out the function using console.trace ()The call stack.

function foo() {
  bar()
}

function bar() {
  console.trace();
}

foo()
Copy the code

Global object

Special global object

Special global objects can be used arbitrarily in modules but not in command-line interactions, including __dirname, __filename, exports, module, require().

  • __dirname: Gets the path of the current file. It is an absolute path, excluding the file name.
  • __filename: Obtains the current file path and filename.
console.log(__dirname);
console.log(__filename);
Copy the code

Commonly used global objects

The process object

The Process object provides information about the Node process, such as the operating environment and parameter information. When we run an application using Node, it starts a process in the operating system.

The console object

The Console object was introduced above.

Timer function

  • SetTimeout (callback, delay[,…args]) : callback executes once after delay milliseconds;
  • SetInterval (callback, delay[,…args]) : Callback is executed every delay millisecond.
  • SetImmediate (callback[,…args]) : The “immediate” execution of a callback after a callbackI/O event;
  • Process.nexttick (callback[,…args]) : adds to the nextTick queue.
setTimeout(() = > {
  console.log('setTimeout');
}, 0)

setInterval(() = > {
console.log('setInterval');
}, 1000)

setImmediate(() = > {
  console.log('setImmediate');
})

process.nextTick(() = > {
  console.log('process.nextTick');
})
Copy the code

Global object

Global is a global object, and the process, console, setTimeout, and so on mentioned above are all placed in the global object.

console.log(global);
Copy the code

Part of the global object is printed on the console, but not all of it.

To see more of the Global object, we can enter the interactive environment at the terminal by typing Node. Then type global in the interactive environment. Press TAB twice to see more information about the Global object.

The difference between Global and Window

In browsers, global variables are on Windows, such as Document, setInterval, setTimeout, alert, console, and so on.

In Node, global is a global object.

Javascript code executed in the browser is added to the Window object by default if we define a property via var at the top-level scope. Because there’s no concept of modules in the browser, it’s the top-level stuff.

var age = 18
console.log(age);
Copy the code

When we run it in a browser and type window in the console, we can see that the Window object has the age property.

In Node, we use var to define a variable, which is just a variable in the current module, not globally. Since each file in Node is a separate module, variables are defined in the module. If placed globally, defining the same variable name in another module will conflict.

We then run this file in the Node environment.

var age = 18
console.log(age);

console.log(global.name);
Copy the code

You can see that there is no name attribute in the Global object.