Nodejs关于循环依赖性的问题
我正在用commonj测试循环依赖性。
//index.js
console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done = %j, b.done = %j', a.done, b.done);
//a.js
console.log('a starting');
exports.done = false;
const b = require('./b.js');
console.log('in a, b.done = %j', b.done);
exports.done = true;
console.log('a done');
//b.js
console.log('b starting');
exports.done = false;
const a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done');
结果是
// output
main starting
a starting
b starting
in b, a.done = false
b done
in a, b.done = true
a done
in main, a.done = true, b.done = true
如此索引
导入a
和a
import b
b
尝试导入a
但 a 未完成,因此a
返回未完成的模块。
好的,我明白了。
但是eSmodule
语法?
我像这样重现了代码。
//index.js
console.log('main starting');
import a from './a.js';
import b from './b.js';
console.log('in main, a.done = %j, b.done = %j', a.done, b.done);
//a.js
console.log('a starting');
export let done = false;
import {done as B_done} from './b.js';
console.log('in a, b.done = %j', B_done);
done = true;
console.log('a done');
export default {
done
}
//b.js
console.log('b starting');
export let done = false;
import {done as A_done} from './a.js';
console.log('in b, a.done = %j', A_done);
done = true;
console.log('b done');
export default {
done
}
但是结果改变了。
ReferenceError:在初始化之前无法访问'a_done'
此错误驱动。
为什么这给我带来的结果与commonjs不同?
Esmodule
返回未完成的模块吗?
您实际上可以在
I was testing circular dependency with commonjs.
//index.js
console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done = %j, b.done = %j', a.done, b.done);
//a.js
console.log('a starting');
exports.done = false;
const b = require('./b.js');
console.log('in a, b.done = %j', b.done);
exports.done = true;
console.log('a done');
//b.js
console.log('b starting');
exports.done = false;
const a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done');
The result was
// output
main starting
a starting
b starting
in b, a.done = false
b done
in a, b.done = true
a done
in main, a.done = true, b.done = true
So index
import a
and a
import b
b
try to import a
but a
is not finished so a
return unfinished module.
OK fine, I understood.
But what about esmodule
syntax ?
I reproduced code like this.
//index.js
console.log('main starting');
import a from './a.js';
import b from './b.js';
console.log('in main, a.done = %j, b.done = %j', a.done, b.done);
//a.js
console.log('a starting');
export let done = false;
import {done as B_done} from './b.js';
console.log('in a, b.done = %j', B_done);
done = true;
console.log('a done');
export default {
done
}
//b.js
console.log('b starting');
export let done = false;
import {done as A_done} from './a.js';
console.log('in b, a.done = %j', A_done);
done = true;
console.log('b done');
export default {
done
}
But the result was changed.
ReferenceError: Cannot access 'A_done' before initialization
This error occrued.
Why is it giving me a different result than commonjs ?
Doesn't esmodule
return unfinished modules?
You can actually find this code sample in here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确实如此。尝试使用
var
而不是让
尝试。因为它也更改了评估顺序。导入是“吊装”的,对模块代码的评估被推迟到设置所有依赖关系(即完全完成评估,或者声明其变量并等待循环依赖项)。与
require()
调用不同,您将无法在a
的中间执行b
的初始化代码。It does. Try with
var
instead oflet
.Because it also changed evaluation order. Imports are "hoisted", the evaluation of the module code is deferred until all dependencies are set up (that is, either completely finished evaluating, or having their variables declared and waiting for circular dependencies). Unlike the
require()
call, you won't get the initialisation code ofb
executing in the middle ofa
.