用指定的值填充满 Buffer 实例
const b = Buffer.allocUnsafe(25).fill('abc呵呵');
// 注意下面因为不够容纳全部的汉字字节,所以乱码
console.log(b.toString()); // abc呵呵abc呵呵abc呵�
Buffer.prototype.includes(value[, byteOffset][, encoding])
Buffer.prototype.indexOf(value[, byteOffset][, encoding])
Buffer.prototype.toJSON(): 返回一个 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;
});









