Which format is each module in in Node.js?

问题: In Node.js, each module is in an object format that has the following properties: Module { id: '.', exports: {}, parent: null, filename: 'C:\Users\Node\first-app...

问题:

In Node.js, each module is in an object format that has the following properties:

Module {
  id: '.',
  exports: {},
  parent: null,
  filename: 'C:\Users\Node\first-app\app.js',
  loaded: false,
  children: [],
  paths:
   [ 'C:\Users\app\node_modules',
     'C:\Users\\Node\node_modules',
     'C:\Users\node_modules',
     'C:\Users\Documents\node_modules',
     'C:\Users\node_modules',
     'C:\Users\node_modules',
     'C:\node_modules' ] }

But, each module is also in a function format as such:

(function (exports, require, module, __filename, __dirname) { 
//content of the function
})

where the function takes exports, require, module, __filename, and __dirname as its parameters. The former is in a key/value format and the latter is in a function definition format. Which one is it?


回答1:

Your code examples are of two different but related things. The first is the module object, which is described here. The second is something that nodes does internally: wrapping a module in an anonymous function, which among other things, helps keep top level variables in your module scoped to that module rather than exposing them globally. This module wrapping behavior is described here.

  • 发表于 2019-03-04 22:35
  • 阅读 ( 171 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除