小编Dam*_*uch的帖子

JS 中的 Importing(require) 模块是否也执行整个代码?

让 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也打印出来的。
导入模块是否包括执行整个模块文件?

javascript module export node.js

2
推荐指数
1
解决办法
1287
查看次数

标签 统计

export ×1

javascript ×1

module ×1

node.js ×1