zoukankan      html  css  js  c++  java
  • nodejs学习笔记-问题记录

    1.nodejs的buffer

    // https://semlinker.com/node-buffer/
    const typedArray3 = new Int8Array(8);typedArray3[0] = -32;
    const typedArray4 = new Int16Array(8);typedArray4[0] = -32;
    const typedArray5 = new Uint8Array(8);typedArray5[0] = -32;// 2^8 - 32
    const typedArray6 = new Uint16Array(8);typedArray6[0] = -32;// 2^16 - 32
    
    console.log(typedArray3);
    console.log(typedArray4);
    console.log(typedArray5);
    console.log(typedArray6);
    
    // 输出:
    > Int8Array [-32, 0, 0, 0, 0, 0, 0, 0]
    > Int16Array [-32, 0, 0, 0, 0, 0, 0, 0]
    > Uint8Array [224, 0, 0, 0, 0, 0, 0, 0]
    > Uint16Array [65504, 0, 0, 0, 0, 0, 0, 0]

    node和浏览器的buffer都是基于js的Uint8Array类生成。

    allocUnsafe和allocSafe的区别是什么?

    allocUnsafe不会对分配的内存进行初始化: https://nodejs.org/dist/latest-v14.x/docs/api/buffer.html#buffer_class_method_buffer_allocunsafe_size

     2.npm包私有作用域

     3.模块函数化和模块对象化

    // 模块函数化
    module.exports = function a(){}
    
    // 模块对象化
    //user.js
    function User(pName){
        this.name=pName;
        this.say=function(){
            console.log('你好,我是隔壁 '+this.name);          
        }
    }
    //将模块导出对象替换为User
    module.exports=User;
    //index.js
    // 引用user.js导出模块
    var User=require('./user.js');
    //创建一个user对象
    var user1=new User('老王');
    //调用这个对象的say方法
    user1.say();

    一个模块中的JS代码仅在模块第一次被使用时执行一次,并在执行过程中初始化模块的导出对象。之后,缓存起来的导出对象被重复利用。这就是commonJs的缓存机制。

    4.sockjs

    SockJS的一大好处在于提供了浏览器兼容性。优先使用原生WebSocket,如果在不支持websocket的浏览器中,会自动降为轮询的方式。

    以下几种轮询方式的解释:https://www.cnblogs.com/hoojo/p/longPolling_comet_jquery_iframe_ajax.html

    https://github.com/sockjs/sockjs-client

    xhr-polling
    jsonp-polling
    iframe-xhr-polling
    xdr-polling †
    WebRTC

     5.换行符(乱入)

    Linux、Windows 和 Mac 中的换行符对比
    对于换行这个动作,Unix下一般只有一个 0x0A 表示换行(" "),Windows 下一般都是 0x0D 和 0x0A 两个字符,即 0D0A(" "),苹果机(MAC OS系统)则采用回车符 CR 表示下一行(" ")。
    Unix 系统中:每行结尾只有 "<换行>",即 " ";
    Windows 系统中:每行结尾是 "<回车><换行>",即 " ";
    Mac 系统中:每行结尾是 "<回车>",即 " "。

    这也是为什么form-data里面的key-value之间要用/r/n换行

     6.在Node.js里面,process.nextTick和Promise优先级

    在Node.js里面,process.nextTick和Promise,谁的优先级更高,why?他们和其他的event loop关系是什么(比如时序上,和setTimeout,setImmediate之类的进行比较)https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

  • 相关阅读:
    Java中Filter、Servlet、Listener的学习 (转)
    Servlet 获取商品信息作业
    Servlet 注册与提交作业
    jsp servlet的区别和联系(转)
    用户注册验证留言程序
    作业1
    简单新闻发布系统
    webform 增速删改查 方法
    存取数据库图片
    打印 保存
  • 原文地址:https://www.cnblogs.com/catherinezyr/p/13476528.html
Copyright © 2011-2022 走看看