ES6早期使用类
ES6 early use of classes
我在 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)。
我是否使用了错误的 pattern/code 结构? 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 检测到。
我在 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)。
我是否使用了错误的 pattern/code 结构? 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 检测到。