Node.js global objects
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);
__dirname represents the directory where the currently executing script is located
[code]console.log("文件所在的目录:" + __dirname);
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);
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("程序执行结束");
[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());

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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!

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

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 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!

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.

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!

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

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!
