Home Web Front-end JS Tutorial Node.js global objects

Node.js global objects

Jan 17, 2017 pm 03:58 PM

Learning points:

- __filename

- __dirname

- setTimeout(cb, ms)

- setInterval(cb, ms)

- clearTimeout(t)

- console

- process

Node.js global object

Global object in Node.js global, all global variables are attributes of the global object. In Node.js, we can directly access the global attributes without including it in the application.

Global objects and global variables

The most fundamental role of global is to serve as the host of global variables.

When we define a global variable, this variable will also become a property of the global object global.

__filename represents the file name of the currently executing script

[code]console.log("文件所在的路径:" + __filename);
Copy after login

__dirname represents the directory where the currently executing script is located

[code]console.log("文件所在的目录:" + __dirname);
Copy after login

setTimeout(cb, ms) delayer object

setInterval(cb, ms) timer object

clearTimeout(t) clear delayer

Case: main.js
[code]console.log("文件所在的路径:" + __filename);
console.log("文件所在的目录:" + __dirname);
var printHello = function () {
    console.log("Hello, World");
}
// 1s后调用函数
var t = setTimeout(printHello, 1000);
// 清除延时器
clearTimeout(t);
Copy after login

Node.js global objects

##console object


Case: console.js

[code]console.info("程序开始执行:");
var counter = 10;
console.log("计数:%d", counter);
console.time("获取数据");
console.timeEnd("获取数据");
console.info("程序执行完毕");
// 当进程准备退出时触发
process.on('exit', function (code) {
    // 以下代码永远不会执行
    setTimeout(function () {
        console.log("该代码不会执行");
    }, 0);
    console.log("退出代码:", code);
});
console.log("程序执行结束");
Copy after login

Node.js global objects

Case 3: process.js

[code]// 输出到终端
process.stdout.write("Hello World!" + "\n");
// 通过参数读取
// argv 属性返回一个数组,由命令行执行脚本时的各个参数组成。
// 它的第一个成员总是node,第二个成员是脚本文件名,其余成员是脚本文件的参数。
process.argv.forEach(function (val, index, array) {
    console.log(index + ": " + val);
    // 0: D:\nodeJS安装包\node.exe
    // 1: E:\node\全局对象\process.js
});
// 获取执行路局
// D:\nodeJS安装包\node.exe
console.log(process.execPath);
// 平台信息
// wind32
console.log(process.platform);
// 输出当前目录
console.log("当前目录:" + process.cwd());
// 输出当前版本
console.log('当前版本:' + process.version);
// 输出内存使用情况
console.log(process.memoryUsage());
Copy after login

Node.js global objects

The above is the content of the Node.js global object. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

How does Node.js do GC (garbage collection)? The following article will take you through it.

Let's talk about how to use pkg to package Node.js projects into executable files. Let's talk about how to use pkg to package Node.js projects into executable files. Dec 02, 2022 pm 09:06 PM

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

See all articles