因此,每个节点模块都包装为函数的主体,如节点源代码中所示
NativeModule.wrapper = [ '(function (exports, require, module, __filename, __dirname) { ', 'n});'];因此,如果您使用声明了一个变量
var,则该变量对于模块而言是函数局部的,基本上是该模块的私有变量。这是不是一个性质
global,
module,
module.exports,或
this。如果您忘记了
var,它将
global作为属性进入对象。如果在上显式创建一个属性
this,则该属性将进入
exports其他模块并可供其他模块使用。
这是一个希望能启发您的小程序。
var aDeclaredVar = '*aDeclaredVar*';undeclaredVar = '*undeclaredVar*';this.aThisProperty = '*aThisProperty*';module.aModuleProperty = '*aModuleProperty*';module.exports.anExportProperty = '*anExportProperty*';console.log('this', this);console.log('this === exports', this === exports);console.log('this === module', this === module);console.log('this === module.exports', this === module.exports);console.log('aDeclaredVar', aDeclaredVar);console.log('undeclaredVar', undeclaredVar);console.log('this.aThisProperty', this.aThisProperty);console.log('module.aModuleProperty', module.aModuleProperty);console.log('module.exports.anExportProperty', module.exports.anExportProperty);console.log('global.undeclaredVar', global.undeclaredVar);console.log('global.aDeclaredVar', global.aDeclaredVar);它的输出是:
this { aThisProperty: '*aThisProperty*', anExportProperty: '*anExportProperty*' }this === exports truethis === module falsethis === module.exports trueaDeclaredVar *aDeclaredVar*undeclaredVar *undeclaredVar*this.aThisProperty *aThisProperty*module.aModuleProperty *aModuleProperty*module.exports.anExportProperty *anExportProperty*global.undeclaredVar *undeclaredVar*global.aDeclaredVar undefined


