gpt4 book ai didi

javascript - 当导入的文件正在导入一个文件,而导入它的文件也在导入时会发生什么?

转载 作者:行者123 更新时间:2023-11-30 19:19:56 25 4
gpt4 key购买 nike

我有三个文件:- main.js <- 依赖项:module.js、helper.js- module.js <- 依赖项:helper.js- helper.js <- 无依赖性

main.js 和 module.js 都是从 helper.js 导入的; main.js 是从 module.js 导入的

main.js 从 module.js 导入的函数使用 helper.js 的函数

当我运行 main.js 时:helper.js 会导入两次,一次由 main.js 导入,一次由 module.js 导入,还是 helper.js 仅由 main.js 导入一次?

主要.js:

    import {someFunction} from "./helper.js";
import {someOtherFunction} from "./module.js";
someFunction();
someOtherFunction();

模块.js:

    import {someFunction} from "./helper.js";
function someOtherFunction(){
// do something using someFunction();
someFunction();
};
export {someOtherFunction};

helper.js:

   function someFunction(){
// do something
};
export {someFunction};

最佳答案

一个模块总是会被加载一次。

ES 262 规范在第 413 页指出:

[Importing a module] must be idempotent if it completes normally. Each time it is called with a specific referencingModule, specifier pair as arguments it must return the same Module Record instance.

Multiple different referencingModule, specifier pairs may map to the same Module Record instance. The actual mapping semantic is implementation-defined but typically a normalization process is applied to specifier as part of the mapping process. A typical normalization process would include actions such as alphabetic case folding and expansion of relative and abbreviated path specifiers.

关于javascript - 当导入的文件正在导入一个文件,而导入它的文件也在导入时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com