node.js之CommonJS

 1、

(
  function(exports, require, module, __filename, __dirname) {
      console.log('This is a test.');  
  }  
);

  - 每个文件是一个模块,有自己的作用域

  - 在模块内部module变量代表模块本身

  - module.exports属性代表模块对外接口

2、require特性

  - module被加载时执行,加载后缓存

  - 一旦出现某个模块被循环加载,就只会输出已经执行的部分,还未执行的部分不输出。开发中要避免两个模块相互加载。

3、node.js将全局属性和方法挂到global上

  - CommonJs

  - Buffer、process、console

  - timer

4、process进程

  - node启动相关一些参数:argv、argv0、execArgv、execPath

const { argv, argv0, execArgv, execPath } = process;

argv.forEach(item => {
    console.log(item);
});

猜你喜欢

转载自www.cnblogs.com/xy-ouyang/p/11614098.html