Does ES6 module importing execute the code inside the imported file?

后端 未结 3 1769
别跟我提以往
别跟我提以往 2020-12-07 18:28

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 defaul         


        
3条回答
  •  自闭症患者
    2020-12-07 18:56

    Yes, it does, exactly one time.

    See 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

提交回复
热议问题