javascript - JavaScript 的 ES6 导入如何处理循环依赖?
问题描述
测试.html
<script type="module" src="./B.js"></script>
js
import {bar} from './B.js';
import {test} from './B.js';
export function foo() {
bar();
}
test();
console.log("IN A");
B.js
import {foo} from './A.js';
export function bar() {
if (Math.random()) {
foo();
}
}
export function test() {
console.log("test");
}
console.log("IN B");
输出是:
B.js:30 test
A.js:24 IN A
B.js:33 IN B
为什么“In B”是执行的最后一行代码?此外,如果在 test.html, 中src="./A.js"
,“IN B”是执行的第一行。
解决方案
推荐阅读
- javascript - 如何在网站上制作带有 svg 元素的块的屏幕截图?
- download - 下载没有 js/css 版本名称的网站的工作本地版本
- coq - 对“N”类型使用“omega”
- angular - 角度类型的BehaviorSubject
- python - 添加时的 Django Admin Model 无法呈现相关的更改链接
- python - 使用 pywinauto 从 wxWindow 控件中获取文本
- python - 为什么这段代码不产生方形二维数组?是什么让它变成三角形?
- python-2.7 - 检查另一个应用程序实例是否正在运行并将该实例置于焦点
- python - 仅绘制 .dat 文件中的一些数据(设置范围)
- php - 如何使用 HTTPRequest 使用 Flutter 登录 Laravel