让 moduleFile.js 为
const calc = {
add(a,b) = {return a+b},
sub(a,b) = {return a-b}
}
console.log(`hello from module`);
exports.calcModule = calc;
Run Code Online (Sandbox Code Playgroud)
并让 main.js 与 moduleFile.js 位于同一目录中
const { calcModule } = require(`./moduleFile`);
console.log(calcModule.add(1,2));
Run Code Online (Sandbox Code Playgroud)
当我在控制台中执行时main.js结果$ node main.js会
像
hello from module
3
Run Code Online (Sandbox Code Playgroud)
我很难理解hello from module也打印出来的。
导入模块是否包括执行整个模块文件?