Skip to content

Commit 4f0c566

Browse files
gqbrefunnycoderstar
authored andcommitted
Update import和require.md
增加一处遗漏代码
1 parent f19caad commit 4f0c566

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

docs/interview/JavaScript/import和require.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,15 @@ ReferenceError: foo is not defined
543543

544544
让我们一行行来看,ES6 循环加载是怎么处理的。首先,执行a.mjs以后,引擎发现它加载了b.mjs,因此会优先执行b.mjs,然后再执行a.mjs。接着,执行b.mjs的时候,已知它从a.mjs输入了foo接口,这时不会去执行a.mjs,而是认为这个接口已经存在了,继续往下执行。执行到第三行console.log(foo)的时候,才发现这个接口根本没定义,因此报错。
545545

546+
```js
547+
// a.mjs
548+
import {bar} from './b.mjs';
549+
console.log('a.mjs');
550+
console.log(bar);
551+
function foo() {return 'foo'};
552+
export {foo};
553+
```
554+
546555
这时再执行a.mjs就可以得到预期结果。
547556
```js
548557
$ node --experimental-modules a.mjs

0 commit comments

Comments
 (0)