【发布时间】:2018-01-28 10:43:52
【问题描述】:
这是我在名为foo.js 的文件中的代码。
console.log('module.exports:', module.exports)
console.log('module.id:', module.id)
console.log('exports:', exports)
console.log('id:', id)
这是我得到的输出。
$ node foo.js
module.exports: {}
module.id: .
exports: {}
/home/lone/foo.js:4
console.log('id:', id)
^
ReferenceError: id is not defined
at Object.<anonymous> (/home/lone/foo.js:4:20)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:598:3
这是我无法理解的。 exports 和 id 都是
module 对象的属性。但我可以访问exports
没有module. 限定符,但我无法为id 这样做
属性。
为什么会这样?这里有什么概念使它成为可能
访问 module.exports 就像 exports 但不是这样
module.id?
【问题讨论】:
标签: javascript node.js variables scope