This article mainly introduces an in-depth understanding of the Nodejs Global module. The editor thinks it is quite good. Now I will share it with you and give it a reference. Let’s follow the editor and take a look.
The browser has its own global object window. Similarly, nodejs also has its own global object global, and it can be directly accessed under each module. Access the global object.
In nodejs, in addition to directly using the native JavaScript functions and objects supported by the V8 JavaScript engine, it also adds some other Functions and objects (for example: Buffer object, require function, etc.).
Buffer object: used to process binary data
##module Object: used to access information of the current module
Buffer object
array in which each element is an integer, but the difference from a real array is that its size is fixed ( That is, the size is determined when the instance is created), and the memory allocated for it is native and exists outside V8's heap memory. Before nodejs version 6.0, the
new Buffer() syntax was used to create an instance. However, due to some security issues, the method of creating an instance in this form was abolished. , replaced by some static methods of some Buffer objects.
set[, length]]): This creates a view of the ArrayBuffer without copying the underlying memory
string[, encoding]): Returns a new Buffer instance initialized by the value of string Buffer instance
const buf1 = Buffer.alloc(5); const buf2 = Buffer.allocUnsafe(5); const buf3 = Buffer.from([1, '2a', 230]); const buf4 = Buffer.from('abcdggg'); console.log(buf1); // <Buffer 00 00 00 00 00> console.log(buf2); // <Buffer b8 ed a3 80 58> (这只是一种可能的结果) console.log(buf3); // <Buffer 01 00 e6> console.log(buf4); // <Buffer 61 62 63 64 67 67 67> console.log(buf4.toString()); // abcdggg buf2.fill(0); console.log(buf2); // <Buffer 00 00 00 00 00>
Buffer .byteLength(string[, encoding]): Return the actual byte length of string (note not the character length)
let str1 = 'a'; let str2 = '小'; let str3 = 'aa'; let str4 = '小a'; console.log(str1.length); // 1 console.log(Buffer.byteLength(str1)); // 1 console.log(str2.length); // 1 console.log(Buffer.byteLength(str2)); // 3 console.log(str3.length); // 2 console.log(Buffer.byteLength(str3)); // 2 console.log(str4.length); // 2 console.log(Buffer.byteLength(str4)); // 4
Buffer.concat(list[, totalLength]): Connect multiple Buffer instances or Uint8Array instances and return a new Buffer instance
const buf1 = Buffer.alloc(10); const buf2 = Buffer.alloc(14); const totalLength = buf1.length + buf2.length; console.log(totalLength); // 24 const buf = Buffer.concat([buf1, buf2], totalLength); console.log(buf.length); // 24
Buffer.isBuffer(obj): Judge whether an object is a Buffer instance
Buffer.isEncoding(encoding):
判断是否支持指定的编码方式
console.log(Buffer.isEncoding('utf8')); // true console.log(Buffer.isEncoding('utf9')); // false
Buffer.poolSize:
指定预分配的字节数的大小,默认为 8192(即 8 KB)
Buffer.prototype.buffer:
一个指向 ArrayBuffer 的引用
const arrayBuffer = new ArrayBuffer(16); const buffer = Buffer.from(arrayBuffer); console.log(buffer.buffer === arrayBuffer); // true
Buffer.prototype.equals(otherBuffer):
比较两个 Buffer 实例是否拥有完全相同的 bytes
const buf1 = Buffer.from('hello'); const buf2 = Buffer.from('hello'); console.log(buf1.equals(buf2)); // true
用于迭代的方法
Buffer.prototype.entries()
Buffer.prototype.keys()
Buffer.prototype.values()
Buffer.prototype.fill(value[, offset[, <a href="http://www.php.cn/wiki/1048.html" target="_blank">end</a>]][, encoding]):
用指定的值填充满 Buffer 实例
const b = Buffer.allocUnsafe(25).fill('abc呵呵'); // 注意下面因为不够容纳全部的汉字字节,所以乱码 console.log(b.toString()); // abc呵呵abc呵呵abc呵�
Buffer.prototype.<a href="http://www.php.cn/wiki/137.html" target="_blank">include</a>s(value[, byteOffset][, encoding])
Buffer.prototype.indexOf(value[, byteOffset][, encoding])
Buffer.prototype.to<a href="http://www.php.cn/wiki/1488.html" target="_blank">JSON</a>():
返回一个 JSON 对象
当 JSON.stringify(buf) 的参数为一个 Buffer 实例时,会隐式地调用上面的方法
const b = Buffer.from('hell') let json = b.toJSON(); console.log(json); // { type: 'Buffer', data: [ 104, 101, 108, 108 ] } console.log(JSON.stringify(b)); // {"type":"Buffer","data":[104,101,108,108]}
Buffer.prototype.toString([encoding[, start[, end]]]):
以指定的 encoding 解码 Buffer 实例,返回解码后的字符串
const buf = Buffer.from([104, 101, 108, 108]); console.log(buf.toString()); // hell console.log(buf.toString('base64')); // aGVsbA== console.log(buf.toString('hex')); // 68656c6c
字符串不能被修改,但是 Buffer 实例却可以被修改。
const buf = Buffer.from('abcd'); console.log(buf.toString()); // abcd buf[1] = 122; console.log(buf.toString()); // azcd
Buffer.prototype.write(string[, offset[, length]][, encoding]):
将指定字符串写入到 Buffer 中
const buf = Buffer.from('abcdefg'); console.log(buf); // <Buffer 61 62 63 64 65 66 67> console.log(buf.toString()); // abcdefg buf.write('和', 1); console.log(buf); // <Buffer 61 e5 92 8c 65 66 67> console.log(buf.toString()); // a和efg
好了,还有一堆方法就不一一列出来了,Buffer 就到这里了。
module 对象
在使用 require 函数加载模块文件时,将运行该模块文件中的每一行代码
模块在首次加载后将缓存在内存缓存区中,所以对于相同模块的多次引用得到的都是同一个模块对象,即对于相同模块的多次引用不会引起该模块内代码的多次执行。
在编译的过程中,Node 会对获取的 JavaScript 文件内容进行头尾包装!
// 包装前 module666.js const PI = 6666; module.exports = PI; // 包装后,注意下面不是立即执行函数 (function(exports, require, module, filename, dirname) { const PI = 6666; module.exports = PI; });
filename & dirname
filename: 返回当前模块文件的绝对路径(带文件名)
dirname: 返回当前模块文件所在目录的绝对路径
// 1.js console.log(filename); // c:\Users\percy\Desktop\nodejs\1.js console.log(dirname); // c:\Users\percy\Desktop\nodejs
Process 对象
process 对象是 nodejs 的一个全局对象,提供当前 nodejs 进程的信息。
process.arch: 返回当前处理器的架构
process.env: 返回一个包含用户环境变量的对象
process.argv: 返回一个数组,数组的第一个元素总是 node 程序的绝对路径,第二个元素是当前执行脚本的绝对路径
process.execPath: 返回 node 程序的绝对路径
process.argv0: 返回 node 程序的绝对路径
process.pid: 返回当前进程的进程号
process.platform: 返回当前的系统平台标识符(比如:'darwin', ‘freebsd', ‘linux', ‘sunos' or ‘win32')
process.version: 返回当前 node 的版本号
process.versions: 返回一个对象,列出了 nodejs 和其相关依赖的版本号
三个重要的属性
process.stdin: 返回一个指向标准输入流的可读流(Readable Stream)
process.stdout: 返回一个指向标准输出流的可写流(Writable Stream)
process.stderr: 返回一个指向标准错误流的可写流(Writable Stream)
方法
process.cwd(): 返回进程当前的工作目录
process.chdir(path): 改变进程当前的工作目录
process.cpuUsage(): 返回当前 CPU 的使用情况
process.memoryUsage(): 返回当前内存的使用情况
process.uptime(): 返回 Node 程序已运行的秒数
process.nextTick(callback[, …args]): 指定回调函数在当前执行栈的尾部、下一次Event Loop之前执行
process.emitWarning(warning[, options]): 触发一个 warning 事件,可以自定义一些警告信息
process.exit([code]): 立即结束当前进程,但是会触发 process 的 exit 事件
process.abort(): 立即结束当前进程,不会触发 exit 事件
console.log(process.cwd()); // c:\Users\percy\Desktop\nodejs process.chdir('../'); console.log(process.cwd()); // c:\Users\percy\Desktop
process.emitWarning('Something happened!', { code: 'MY_WARNING', detail: 'This is some additional information' }); process.on('warning', (warning) => { console.log(warning); })
process.on('exit', function(code) { console.log('exit~', code); }); process.exit(); // exit~
process 对象还有一些方法没列出来,因为我现在看不懂怎么用,以后补 >_<
Console 对象
这个对象就是用来在控制台下面打印一些信息而已,挑几个有用但没记牢的方法来玩玩。
console.dir(value):
打印一个对象的详细信息
const buf = Buffer.from('abcdefg'); console.log(buf); // <Buffer 61 62 63 64 65 66 67> console.dir(buf); // Buffer [ 97, 98, 99, 100, 101, 102, 103 ]
console.time(label) & console.timeEnd(label):
用来统计代码执行时间
let label = 'time'; let str = 'hello'; console.time(label); while (str.length < 999999) { str += 'a'; } console.timeEnd(label); // time: 133.724ms
6 个计时器函数
在浏览器上,就有相应的 4 个计时器函数(setInterval、clearInterval、setTimeout、clearTimeout),只不过它们是 window 全局对象的属性。
在 nodejs 中,除过上面的 4 个计时器,还增加了两个(setImmediate,clearImmediate)。
这六个计时器函数被定义在了全局对象 global 下,即可以直接在代码中进行使用。
The above is the detailed content of In-depth understanding of the Global module in Nodejs. For more information, please follow other related articles on the PHP Chinese website!