buffer
无需引用,node内置
-
parseInt('f', 16)
结果为15 -
Buffer.alloc(5)
分配一个大小为5的buffer -
Buffer.from('如意')
创建一个内容为“如意”的buffer(一个汉字占3个字节) -
buf.fill(0)
将此buffer填充为0 -
buf.write('浣碧', 0, 6, 'utf8')
向buffer中写入数据 -
buf.writeInt8(97, 0)
以十进制的ascii值进行写入 -
buf.toString('utf8', 0, 3)
=> 如 - 有 slice、subarray、copy 和
Buffer.concat([buf1, buf2])
、Buffer.isBuffer(buf)
等方法 - base64
util
- util.inherits 一个假继承,只能继承链上的属性和方法
let util = require('util')
function A () { }
A.prototype.fn = () => {}
function B () { }
// B.prototype.__proto__ = A.prototype
// Object.setPrototypeOf(B.prototype, A.prototype)
// B.prototype = Object.create(A.prototype)
util.inherits(B, A)
console.log(new B().fn)
- promisify将方法promise化。仅node中有异步方法可调,因为它们的回调函数入参都是(err, data)
let fs = require('fs')
let readFilePromisify = util.promisify(fs.readFile)
readFilePromisify('./aa.txt', 'utf8').then(data => {}, err => {})
let fs = require('fs').promisify // 还可以这样
events
const EventEmitter = require('events');
function myEmitter () {
}
util.inherits(myEmitter, EventEmitter)
let me = new myEmitter()
me.on('newListener', type => {
console.log('newListener', type)
})
me.on('order1', msg => console.log('order1', msg))
me.emit('order1', 'haha')
me.once('once', msg => console.log('once', msg))
me.emit('once', 'onceonce')
me.emit('once', 'onceonce')
let fn = msg => console.log('order2', msg)
me.on('order2', fn)
me.emit('order2', 'off event')
me.off('order2', fn)
me.emit('order2', 'off event')
自己来实现一个,它要有:
- 链上要有on方法
- 链上要有emit方法
- 链上要有off方法
- 链上要有once方法,只会触发一次的事件
当 eventName 事件下次触发时,监听器会先被移除,然后再调用
- 特殊事件newListener,当有新事件订阅时,默认触发这个事件
function myEvent () {
this.eventList = new Map()
}
myEvent.prototype.on = function (type, fn) {
if (this.eventList.get(type))
this.eventList.get(type).add(fn)
else
this.eventList.set(type, new Set([fn]))
if (type != 'newListener') {
this.emit('newListener', type)
}
}
myEvent.prototype.emit = function (type, arg) {
this.eventList.get(type) && this.eventList.get(type).forEach(fn => {
if (fn.onceEvent) fn.onceEvent(arg) // 执行once事件
else fn(arg)
});
}
myEvent.prototype.off = function (type, fn) {
this.eventList.get(type) && this.eventList.get(type).delete(fn)
}
myEvent.prototype.once = function (type, fn) {
fn.onceEvent = (arg) => {
this.off(type, fn)
fn(arg)
}
this.on(type, fn)
}
module.exports = myEvent
FS
- readFileSync 默认返回编码 buffer
- writeFileSync 默认返回编码 utf8
- open(path[, flags[, mode]], callback)
flag: a r w r+ w+
mode: 777:所有人所有权限(2读4写1执行;[我][本组][所有组]) - read
- write
- mkdir、rmdir、readdirSync、unlink
- fstatSync 获取文件信息
// 异步目录删除 广度遍历
let fs = require('fs')
let path = require('path')
let next = count => fn => --count == 0 && fn()
let DirDeepDel = function (dir, callback) {
fs.stat(dir, (err, stats) => {
if (stats.isFile()) {
return setTimeout(() => fs.unlink(dir, callback), 30) // 当前路径是文件,直接删除
}
fs.readdir(dir, (err, files) => {
if (files.length == 0) {
return setTimeout(() => fs.rmdir(dir, callback), 3000) // 当前路径下为空,直接删除
}
let nextfn = next(files.length)
files.forEach(file => {
DirDeepDel(path.join(dir, file), err => {
if (!err) nextfn(() => fs.rmdir(dir, callback))
})
})
})
})
}
// 预期效果
DirDeepDel('./1', () => {})
网友评论