javascript - ES6 模块导入是否执行导入文件中的代码?

标签 javascript ecmascript-6 es6-modules

js 文件中的代码是否在导入过程中运行?如果是,那么是一次还是每次? 例如

// a.js
console.log("A");
const a = "a"; 
export default a;

// b.js
import a from "./a"; // => console logs?

// c.js
import a from "./a"; // => console logs again?

最佳答案

是的,确实如此,恰好一次。

参见 http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-module-records :

Do nothing if this module has already been evaluated. Otherwise, transitively evaluate all module dependences of this module and then evaluate this module

关于javascript - ES6 模块导入是否执行导入文件中的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37325667/

相关文章:

javascript - 为什么我的输入被插入到具有相同索引的两个不同数组中? (js)

javascript - "import * as"是否比特定的命名导入效率低?

javascript - ESLint 说数组从未被修改,即使元素被插入数组

javascript - 浏览器中的 ES6 模块 : Uncaught SyntaxError: Unexpected token import

javascript - 异步等待问题

javascript - TypeError : $scope. array.reduce 不是一个函数

javascript - 在没有jQuery的情况下悬停父绝对div的子元素时防止onmouseout

ecmascript-6 - 带箭头功能的 ES6 getter/setter

reactjs - 为什么要导入React

javascript - 是否可以通过 cdn 在浏览器中使用 react-select?