• 学习node2_process,buffer


    1:process全局对象 process.argv

    第一个是node,第二个是运行的文件 如果命令行加参数,就是加在数据后面

    2:process.version===node -v 查看版本信息      process.versions 查看node以及node依赖包版本信息console.log=process.stdout.write()一个指向标准输出流(number貌似第二个输出不了)

    process.on('exit', function () {
      setTimeout(function () {
        console.log('This will not run');
      }, 100);
      console.log('Bye.');
    });
    
    结束进程会调用  但是setTimeOut里面的永远不会执行
    

      

    process.on('uncaughtException', (err) => {
      fs.writeSync(1, `Caught exception: ${err}
    `);
    });
    
    setTimeout(() => {
      console.log('This will still run.');
    }, 500);
    
    // Intentionally cause an exception, but don't catch it.
    nonexistentFunc();
    console.log('This will not run.');
    
    
    我们来看上面的例子,我们注册了uncaughtException事件来捕捉系统异常。执行到nonexistentFunc()时,因为该函数没有定义所以会抛出异常。因为javascript是解释性的语言,nonexistentFunc()方法上面的语句不会被影响到,他下面的语句不会被执行。所以他的执行结果如下:
    
    Caught exception: ReferenceError: nonexistentFunc is not defined
    This will still run.
    

      

    3:process.stdin.resume();

    process.stdin.on('data',function(chunk){
    console.log('用户输入了:'+chunk)     监听用户输入的信息
    })

    4:new Buffer(size);size[Number]创建一个Buffer对象,并为这个对象分配一个大小;

    当我们为一个Buffer对象分配空间大小以后,其长度是固定,不能更改的。

    5:var bf=new Buffer('yuling','utf-8');

    bf.forEach(function(a){
    console.log(String.fromCharCode(a))
    })

    6:如果想获取一段内容的字节长度可以用Buffer来做,获取内容长度可以用字符串方法

    7:把字符串写入Buffer对象里面,var str='yuling';var bf=new Buffer(5); bf.write(str,1)是从bf的第1索引开始写入 索引为0的为空;

    Buffer.write(要写入的字符串,从Buffer对象中的几位开始写,写入的字符串的长度,写入字符串的编码)

    8:new Buffer.toString('uft-8',1)从索引1开始截取并按照‘utf-8’转化为字符串。(中文一个字是三个字符,所以需要注意,否则出现乱码);

    9:Buffer.toJSON();返回一个JSON表示的Buffer实例。JSON.stringify转化为JSON字符串

    10:Buffer.slice()和字符串方法相同 但是获取的Buffer引用的还是原来的Buffer地址,修改其中一个,另一个也会变(感觉有点坑爹,浅复制);

    11:Buffer.copy()深拷贝, bf1.copy(bf2,num,num_bf1s,num_bf1e)  b1的数据拷贝到bf2(看着很奇怪),num是拷贝的数据从bf2的索引没num开始覆盖,(num前面不不变)(num_bf1s,num_bf1e代表的是bf1的数据从哪开始拷贝,从哪结束)

  • 相关阅读:
    C++test 关于resource参数和include/exclude参数说明
    开发程序 ip 127.0.0.0 0.0.0.0原理与区别
    安装rails旧版本出错bin_path': can't find gem railties (["3.0.5"]) with executable rails (Gem::GemNotFoundException)
    rails excel的创建
    ruby nil? empty? blank? 的区别
    rails 调试工具pry 换掉debugger 和 rails c
    rails respond_to 的原理与使用
    rails transaction 的用法
    ssh的传送文件命令
    出现了pid的错误A server is already running. 和如何改变webrick的端口值
  • 原文地址:https://www.cnblogs.com/yudabing/p/6992095.html
Copyright © 2020-2023  润新知