• Node.js Video Tutorials

Node.js - 全域性物件



Node.js 中的全域性物件是內建物件。Node.js 執行時由許多核心模組組成。要使用任何核心模組(例如 fs 模組或 http 模組)或從 npm 安裝的任何外部模組(例如 express 模組)的功能,您需要使用 require() 函式載入它們。但是,某些模組、函式、類、變數等可以直接在 JavaScript 程式碼中使用,無需使用 require() 函式載入它們。它們被稱為全域性物件。讓我們看看 Node.js 中的全域性類、變數和函式。

Buffer 類

Buffer 類是一個全域性類,可以在應用程式中訪問,而無需匯入 buffer 模組。Buffer 類的一個物件儲存原始資料,類似於整數陣列,但對應於 V8 堆之外的原始記憶體分配。

您可以建立一個 10 個位元組的未初始化 Buffer:

var buf = new Buffer(10);

也可以從給定的陣列建立 Buffer 物件:

var buf = new Buffer([10, 20, 30, 40, 50]);

或從給定的字串建立:

var buf = new Buffer("Simply Easy Learning", "utf-8");

您可以對 Buffer 執行不同的操作,例如讀取/寫入資料、將 Buffer 轉換為 JSON、連線 Buffer 等。

Console 類

Console 類包含諸如 console.log()、console.error() 和 console.warn() 等方法,可用於寫入任何 Node.js 流。全域性 console 可在不呼叫 require('node:console') 的情況下使用。

以下 REPL 會話顯示了全域性 console 物件的功能

> console.log("Hello World");
Hello World
undefined
> console.error("Some error occurred");
Some error occurred
undefined
> console.warn("This is a warning message!");
This is a warning message!
undefined

Process 物件

process 物件是一個全域性物件,儘管它是在 process 模組中定義的。它是 EventEmitter 類的例項。process 物件提供有關當前程序的資訊。藉助與該物件關聯的大量方法和屬性,可以控制當前程序。

Process 物件的屬性之一是 argv 陣列。它儲存傳遞給 node 可執行檔案的命令列引數。

陣列中的第 0 個元素是 node 可執行檔案,第一個元素是 javascript 檔案,後面是傳遞的引數。

將以下指令碼另存為 hello.js 並從命令列執行它,從命令列向它傳遞一個字串引數。

const args = process.argv;

console.log(args); 

const name = args[2];

console.log("Hello,", name);

在終端中,輸入

PS D:\nodejs> node hello.js TutorialsPoint
[ 'C:\\nodejs\\node.exe', 'D:\\nodejs\\a.js', 'TutorialsPoint' ]
Hello, TutorialsPoint

process.argv0 屬性儲存 Node.js 啟動時傳遞的 argv[0] 的原始值的只讀副本。

Process 物件的 env 屬性儲存環境變數。您可以從命令列設定環境變數。在 node 可執行檔名之前為一個或多個變數賦值。

USER_ID=101 USER_NAME=admin node app.js

在指令碼內部,環境變數作為 process.env 物件的屬性可用

process.env.USER_ID; // "101"
process.env.USER_NAME; // "admin"

有關當前程序的一些有用資訊儲存在 process 物件的屬性中,如下例所示。

示例

console.log('Process Architecture:'+process.arch);  
console.log('Current working directory:'+ process.cwd());
console.log('Process PID: '+process.pid);  
console.log('Process Platform: '+process.platform);  
console.log('Process Version: '+process.version);  

輸出

Process Architecture:x64
Current working directory:D:\nodejs
Process PID: 11060
Process Platform: win32
Process Version: v20.9.0

全域性計時器函式

Node.js 的 timer 模組定義了用於排程回撥的函式。它們可以用作全域性函式(無需匯入模組)。

setTimeout() 函式用於在指定的毫秒數後執行回撥。

function printHello() {
   console.log( "Hello, World!");
}

// Now call above function after 2 seconds
setTimeout(printHello, 2000);

clearTimeout() 函式用於停止先前使用 setTimeout() 建立的計時器。

setInterval() 函式用於在指定的毫秒數後重復執行回撥。

function printHello() {
   console.log( "Hello, World!");
}

// Now call above function after 2 seconds
setInterval(printHello, 2000);

全域性變數

__filename

__filename 表示正在執行的程式碼的檔名。這是此程式碼檔案的解析絕對路徑。對於主程式,這並不一定與命令列中使用的檔名相同。模組內部的值是該模組檔案路徑。

示例

建立一個名為 main.js 的 js 檔案,其中包含以下程式碼:

// Let's try to print the value of __filename
console.log( __filename );

現在執行 main.js 以檢視結果:

D:\nodejs\main.js

__dirname

__dirname 表示當前正在執行的指令碼所在的目錄的名稱。

示例

建立一個名為 main.js 的 js 檔案,其中包含以下程式碼:

// Let's try to print the value of __dirname
console.log( __dirname );

現在執行 main.js 以檢視結果:

D:\nodejs

請注意,用於將給定模組匯入 Node.js 執行時的 require() 函式也是一個全域性函式。

全域性物件

下表列出了我們在應用程式中經常使用的其他物件。有關更多詳細資訊,您可以參考官方文件。

序號 模組名稱和描述
1 控制檯

用於在 stdout 和 stderr 上列印資訊。

2 程序

用於獲取有關當前程序的資訊。提供與程序活動相關的多個事件。

廣告

© . All rights reserved.