Let's say we have four modules, A
, B
,C
and D
In module A
:
console.log("A evaluated")
function AClass {
console.log("A constructor")
}
var aObj = new AClass()
export default aObj;
In module B
:
import aObj from A
export default "B"
In module C
:
import aObj from A
export default "C"
In module D
:
import b from B
import c from C
import aObj from A
So when module D
is evaluated, how many times will the A evaluated
and A constructor
be printed in the console?
Is this behavior described in ES6 standard? What should I do if I want a module to be evaluated ONLY ONCE no matter how many times is imported directly or indirectly? Does anyone have any ideas about this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…