ES6模块导入是否在导入文件中执行代码? [英] Does ES6 module importing execute the code inside the imported file?

查看:102
本文介绍了ES6模块导入是否在导入文件中执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does the code inside the js file gets run during the import? if yes, then once or each time? e.g.

// 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

这篇关于ES6模块导入是否在导入文件中执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆