CommonJS

作者: 大香蕉s | 来源:发表于2017-04-05 15:43 被阅读0次

    默认每个文件的变量为私有,作用域只在这一文件中
    要共享变量:用 global

    module 变量代码当前模块
    exports xx 导出对外接口

    特点:

    1. 模块化作用域
    2. 具有缓存机制,多次加载只运行一次,即第二次加载时读取缓存结果
    3. 多模块加载的顺序按照其在代码中的顺序

    规则:
    require(xxx.js) 可省略 .js 后缀,即 require(xxx)

    模块搜索顺序

    require('bar.js')
    1. /usr/local/lib/node/bar.js      // 若找不到,下步
    2. ./node_module/bar,js          // 若找不到,下步
    3. /home/user/node_module/bar.js
    4. /home/node_module/bar.js
    5. /node_module/bar.js
    

    相关文章

      网友评论

          本文标题:CommonJS

          本文链接:https://www.haomeiwen.com/subject/hqntattx.html