简体   繁体   中英

Circular dependencies in ES6/7

I was surprised to find that in Babel, I could have two modules import each other without any issues. I have found a few places that refer to this as a known and expected behaviour in Babel. I know that this is widely considered an anti-pattern by a lot of (I'm guessing most) people, but please ignore that for this question:

Does anyone know if this is (or will be) correct behaviour in ES6/7?

The closest thing I can find to an official answer (and technical explanation) is this comment on 2ality.com

It's usually considered bad practices. But ES modules are designed to support cyclic dependencies. There are some information in ES 2017 spec 15.2.1.16.3ResolveExport Concrete Method . However, I think it's really hard to read (to be honest, I suffer a lot). The post you mentioned from 2ality.com might be better readable.

I work with nodeJS, so it's kinda the same. From what I know, "require cycles" are not illegal, even if it's often best to avoid them. If there is a cycle, nodeJS at least will handle that: it will not require the second time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM