我在 ES6 中有两个相互需要的类。代码在这里(我使用 Meteor.js);
export class Activity {
constructor(doc) {
_.extend(this, doc);
}
getSubActivities() {
return Activities.find({ parent: this._id }).fetch();
}
}
class ActivitiesCollection extends Mongo.Collection {
/* nothing relevant */
}
export const Activities = new ActivitiesCollection('appjournalActivities', {
transform(doc) {
return new Activity(doc);
},
});
代码工作正常。但 linter 不喜欢我在定义之前使用 Activities
( error description at eslint.org )。
我使用了错误的模式/代码结构吗? ES6 中有标准解决方案吗?
一种解决方案如下(有效):
export let Activities = null;
/* code as before */
Activities = new ActivitiesCollection('appjournalActivities', {
/* code as before */
但是我不太喜欢它。
最佳答案
The code works properly. But the linter does not like it.
然后修改 linter,或者禁用该特定规则:-)
Am I using a wrong pattern/code structure?
没有。如果你有循环依赖,这几乎是不可避免的。
Is there a standard solution in ES6?
您可以将它们放在单独的模块中,这只会将循环依赖关系从范围提升到模块级别,但 es-lint 可能无法检测到。
关于javascript - ES6早期使用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37144786/