-
poolSize:内存载体的容量 isBuffer:是否为buffer类型对象 compare:用来判断两个buffer对象的相对位置 isEncoding:判断nodejs是否支持某种编码 concat:将几个buffer对象连接创建一个新的buffer对象 byteLength:获得指定编码下字符串所占的字节数查看全部
-
Buffer在nodejs中用来处理二进制的数组(js字符串是用utf-8存储的,处理二进制的能力是很弱的,而网络层对资源的请求,响应等基本以二进制来进行交互)创建一个专门存储二进制的缓存区,并提供了一些方法对这些缓存区的数据做进一步的处理 buffer在nodejs里可全局访问 buffer实例化 1. new Buffer('hello 你好');//以默认编码格式utf-8进行字符转换 2. new Buffer('hello 你好','base64');//将默认编码格式修改为base64 3. var buf = new Buffer(8);//设置缓存区的大小 buf.length; //8 4. var buf = new Buffer('12345678'); console.log(buf) //buf长度为8 5. var buf = new Buffer(7); buf.write('12345678'); console.log(buf) //只要指定了buf长度,超出了都不会被缓存 6. var buf = new Buffer([1,2,3,4]);//经过数组初始化 console.log(buf[1])//值为2. 可以通过下标来访问,如果值为小数,会直接取整数。查看全部
-
buffer缓冲,在nodejs里处理二进制的数据。为什么要有buffer呢,因为js的字符串是以utf-8的编码存储的,处理二进制的能力是很弱的,而网络层对于不同资源、文件的请求、响应都是用二进制这种方式进行交互的,所以nodejs就有一个接口来创建存放二进制数据的缓存区,并提供一些方法来对缓存区的数据进行进一步的处理。Buffer在nodejs中是可以全局访问的,不需要require来加载。查看全部
-
marginLeft == disdance时,为什么我的要加return才能执行?查看全部
-
promise 的三种状态 未完成 已完成 失败查看全部
-
function promiseAnimate(ball,distance){ return new Promise(function(resolve,reject){ function _antimate(){ window.setTimeout(function(){ var marginLeft = parseInt(ball.style.marginLeft,10); if(marginLeft===distance){ return resolve; }else{ if(marginLeft<distance){ marginLeft++; }else if(marginLeft>distance){ marginLeft--; } ball.style.marginLeft=marginLeft+'px'; _antimate(); } },13); }; _antimate(); }); promiseAnimate(ball1, 100) .then(function(){ return promiseAnimate(ball2, 200) }) .then(function(){ return promiseAnimate(ball3, 300) }) .then(function(){ return promiseAnimate(ball3, 150) }) .then(function(){ return promiseAnimate(ball3, 150) }) .then(function(){ return promiseAnimate(ball1, 150) })查看全部
-
对于JavaScript来说,对Unicode编码的数据很容易处理的但是对于二进制数据就没什么处理方法了。但是在一些TCP数据流或者在操作一些文件数据流的时候,字节流的处理方法还是必须的。nodeJS中便出了处理的策略方法~提供了与String类似对等的全局构造函数Buffer(与其说与String对等,还不如说与Array类似但是有些不同方面还是需要注意),全局那么自然就不要每次的require了。Buffer则node中储存二进制数据的中介者。查看全部
-
Stream的种类 1. Readable (可读流) pause, resume 2. Writable (可写流) 3. Duplex (双工流) 4. Transform (转换流) 可读可写, 不保存数据, 处理流经的数据查看全部
-
buffer_image.js代码中 logo.png慕课网下载,fs.readFile后返回的origin_buffer为buffer,如果引用自己的图片,不能保证origin_buffer是buffer查看全部
-
所有的流都是EventEmitter的实例,也就是说流是异步的,支持事件监听,可以监听流任何阶段的变化查看全部
-
Buffer用来保存原始数据,流是用来暂存和移动数据的查看全部
-
function promiseAnimate(ball,distance){ return new Promise(function(resolve,reject){ function _antimate(){ window.setTimeout(function(){ var marginLeft = parseInt(ball.style.marginLeft,10); if(marginLeft===distance){ return resolve; }else{ if(marginLeft<distance){ marginLeft++; }else if(marginLeft>distance){ marginLeft--; } ball.style.marginLeft=marginLeft+'px'; _antimate(); } },13); }; _antimate(); });查看全部
-
npm bluebird查看全部
-
var Promise = window.Promise; function promiseAnimate(ball, distance) { return new Promise(function(resolve, reject){ function _animate (ball, distance){ setTimeout(function(){ var marginLeft = parseInt(ball.style.marginLeft, 10); if(marginLeft === distance){ //需要运行 resolve 改为 resolve(); resolve(); return; } else { if(marginLeft < distance){ marginLeft ++; }else{ marginLeft --; } } ball.style.marginLeft = marginLeft + 'px'; _animate(ball, distance); }, 13); }; _animate(ball, distance); }) }; promiseAnimate(ball1, 100) .then(function(){ return promiseAnimate(ball2, 200) }) .then(function(){ return promiseAnimate(ball3, 300) }) .then(function(){ return promiseAnimate(ball3, 150) }) .then(function(){ return promiseAnimate(ball3, 150) }) .then(function(){ return promiseAnimate(ball1, 150) })查看全部
-
var fs=require('fs'); fs.readFile('logo.png',function(err,origin_buffer){ console.log(Buffer.isBuffer(origin_buffer))//判断是否为缓冲区 fs.writeFile('logo_buffer.png',origin_buffer,function(err){//将开始的buffer得到一个新的new——image if(err) console.log(err); }) var base64Image=origin_buffer.toString('base64'); console.log(base64Image); var decodeImage=new Buffer(base64Image,'base64'); console.log(Buffer.compare(decodeImage,origin_buffer)); fs.writeFile('logo_decode.png',decodeImage,function(err){ if(err) console.log(err); }) })查看全部
举报
0/150
提交
取消