node全局对象相关
- 全局对象相关
// 1.全局对象global,和浏览器中的window相似
console.log(global);
========>>打印结果
Object [global] {
global: [Circular *1],
queueMicrotask: [Function: queueMicrotask],
clearImmediate: [Function: clearImmediate],
setImmediate: [Function: setImmediate] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
},
structuredClone: [Function: structuredClone],
clearInterval: [Function: clearInterval],
clearTimeout: [Function: clearTimeout],
setInterval: [Function: setInterval],
setTimeout: [Function: setTimeout] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
},
atob: [Function: atob],
btoa: [Function: btoa],
performance: Performance {
nodeTiming: PerformanceNodeTiming {
name: 'node',
entryType: 'node',
startTime: 0,
duration: 34.0697078704834,
nodeStart: 2.216874837875366,
v8Start: 6.030207872390747,
bootstrapComplete: 28.79891586303711,
environment: 17.475457906723022,
loopStart: -1,
loopExit: -1,
idleTime: 0
},
timeOrigin: 1684209790544.549
},
fetch: [AsyncFunction: fetch]
}
2.全局对象,引用自己
和【window === window.window】一样 console.log(global === global.global) ==》 ture;
3.timer是个对象, 在浏览器环境是数子ID
const timer = setTimeout(() => { console.log(1); }, 1000); console.log(timer);
4.类似于 setTimeout(fn,0)
const timer = setImmediate(() => { console.log(1); });
console.log(timer); // 输出对象
========>>打印结果
Timeout {
_idleTimeout: 1000,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 15,
_onTimeout: [Function (anonymous)],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(refed)]: true,
[Symbol(kHasPrimitive)]: false,
[Symbol(asyncId)]: 2,
[Symbol(triggerId)]: 1
}
5.console, 没啥好说的
6.__dirname 获取当前模块所在的目录 【并非global的属性】
console.log(__dirname);
// /Users/fuxing/Desktop/project/code/web/node/node_xuexi
console.log(global.__dirname);// undefined
7.__filename 获取当前模块文件的路径 【并非global的属性】
console.log(__filename);
// /Users/fuxing/Desktop/project/code/web/node/node_xuexi/demo001.js
8.Buffer 类型化数组 继承自 UIntArray
- process 进程
9.1 process.cwd() 返回当前nodejs进程的工作目录 绝对路径
console.log("当前命令行: ", process.cwd());
// /Users/fuxing/Desktop/project/code/web/node
9.2 process.exit() 强制退出当前的node进程 。可传入退出码,0标识成功退出,默认0
setTimeout(() => { console.log("abc"); }, 1000);
process.exit(0);
9.3 process.argv 【string[] 获取命令中的所有参数】
console.log(process.argv);
终端运行: node demo001 1 2 3 a b c
输出
[
'/usr/local/bin/node',
'/Users/fuxing/Desktop/project/code/web/node/node_xuexi/demo001',
'1',
'2',
'3',
'a',
'b',
'c'
]
9.4 process.platform 获取当前的操作系统
console.log(process.platform);
// darwin/win32 Mac的操作系统
9.5 process.kill() 根据进程ID杀死进程
process.kill(5786);
9.6 process.env 获取环境变量 --- 很有用的
console.log(process.env);
========>>打印结果
{
MANPATH: '/Users/fuxing/.nvm/versions/node/v18.13.0/share/man:/opt/homebrew/share/man::',
VSCODE_CRASH_REPORTER_PROCESS_TYPE: 'extensionHost',
NVM_CD_FLAGS: '-q',
SHELL: '/bin/zsh',
HOMEBREW_BOTTLE_DOMAIN: 'https://mirrors.ustc.edu.cn/homebrew-bottles/bottles',
TMPDIR: '/var/folders/dn/c54d7cqd2gscfn61khs9mdfm0000gn/T/',
HOMEBREW_REPOSITORY: '/opt/homebrew',
ORIGINAL_XDG_CURRENT_DESKTOP: 'undefined',
MallocNanoZone: '0',
USER: 'fuxing',
NVM_DIR: '/Users/fuxing/.nvm',
COMMAND_MODE: 'unix2003',
SSH_AUTH_SOCK: '/private/tmp/com.apple.launchd.idNkN5Z5Mh/Listeners',
__CF_USER_TEXT_ENCODING: '0x1F5:0x19:0x34',
ELECTRON_RUN_AS_NODE: '1',
VSCODE_AMD_ENTRYPOINT: 'vs/workbench/api/node/extensionHostProcess',
PATH: '/Users/fuxing/.nvm/versions/node/v18.13.0/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin',
_: '/Users/fuxing/.nvm/versions/node/v18.13.0/bin/node',
__CFBundleIdentifier: 'com.microsoft.VSCode',
PWD: '/Users/fuxing/Desktop/project/code/web/node',
VSCODE_HANDLES_UNCAUGHT_ERRORS: 'true',
XPC_FLAGS: '0x0',
XPC_SERVICE_NAME: 'application.com.microsoft.VSCode.42889349.42889355',
SHLVL: '1',
HOME: '/Users/fuxing',
VSCODE_NLS_CONFIG: '{"locale":"zh-cn","osLocale":"zh-cn","availableLanguages":{"*":"zh-cn"},"_languagePackId":"07d862891cffead1799ff1d0d31a0385.zh-cn","_translationsConfigFile":"/Users/fuxing/Library/Application Support/Code/clp/07d862891cffead1799ff1d0d31a0385.zh-cn/tcf.json","_cacheRoot":"/Users/fuxing/Library/Application Support/Code/clp/07d862891cffead1799ff1d0d31a0385.zh-cn","_resolvedLanguagePackCoreLocation":"/Users/fuxing/Library/Application Support/Code/clp/07d862891cffead1799ff1d0d31a0385.zh-cn/6a995c4f4cc2ced6e3237749973982e751cb0bf9","_corruptedFile":"/Users/fuxing/Library/Application Support/Code/clp/07d862891cffead1799ff1d0d31a0385.zh-cn/corrupted.info","_languagePackSupport":true}',
HOMEBREW_PREFIX: '/opt/homebrew',
LOGNAME: 'fuxing',
VSCODE_IPC_HOOK: '/Users/fuxing/Library/Application Support/Code/1.78-main.sock',
VSCODE_CODE_CACHE_PATH: '/Users/fuxing/Library/Application Support/Code/CachedData/6a995c4f4cc2ced6e3237749973982e751cb0bf9',
NVM_BIN: '/Users/fuxing/.nvm/versions/node/v18.13.0/bin',
VSCODE_PID: '79115',
INFOPATH: '/opt/homebrew/share/info:',
HOMEBREW_CELLAR: '/opt/homebrew/Cellar',
VSCODE_CWD: '/'
}