[英]In Node/raw ES6, how could I emit an event from a parent class and trigger an event in the child class from their respective constructors?
I'm currently working on exercism.io's "JS track - react project" (no not that react).我目前正在研究 exercism.io 的“JS 轨道 - 反应项目”(不,不是反应)。 The exercise is to make classes that react to one another's actions.
练习是让类对彼此的行为做出反应。 I wasn't sure if inheritance was the best bet, but I feel like it's as close as I'm going to get.
我不确定继承是否是最好的选择,但我觉得它就像我要得到的一样接近。 (PS This is my first post here, I tried quite a few places before deciding to ask my first question here. ('PPS') => 'Howdy' :)
(PS 这是我在这里的第一篇文章,在决定在这里问我的第一个问题之前,我尝试了很多地方。('PPS')=> '你好' :)
`class InputCell extends EventEmitter {
constructor(value) {
super(value);
this.value = value;
}
setValue(value) {
this.value = value;
// I'm assuming this emitter doesn't leave the scope of the object it creates?
this.emit('cellChange');
}
}
class ComputeCell extends InputCell {
constructor(cells, compute) {
super(cells, compute);
this.value = compute(cells);
// super wasn't my first choice, but 'this' didn't work either
super.on('cellChange', () => compute(cells));
}
}`
Edit- My apologies, here is the failing test I run.编辑 - 抱歉,这是我运行的失败测试。
` test('compute cells update value when inputs are changed', () => {
const inputCell = new InputCell(1);
const computeCell = new ComputeCell(
[inputCell], inputs => inputs[0].value + 1
);
inputCell.setValue(3);
expect(computeCell.value).toEqual(4);
});
` `
@TJ Allen: I'm stuck on the same exercise, but I found the solutions page:https://exercism.io/tracks/javascript/exercises/react/solutions @TJ Allen:我被困在同一个练习中,但我找到了解决方案页面:https ://exercism.io/tracks/javascript/exercises/react/solutions
it took some looking but I found this explanation to be simple enough for my understanding (specifically for the test case you posted):花了一些时间,但我发现这个解释对于我的理解来说足够简单(特别是对于您发布的测试用例):
export class InputCell {
constructor(value) { // test 1: accepts input
this.value = value;
}
setValue(value) {
this.value = value; // test 2: allows input value to be set
}
}
export class ComputeCell {
constructor(inputCells, func) { // test 3: allows setting compute cells
this.inputs = inputCells;
this.func = func;
}
get value() {
return this.func(this.inputs); // test 5: compute cells update value when inputs are changed
}
}
For other exercism.io users, note that I've left-out a comment for test 4: compute cell takes inputs in correct order as it was less relevant to the question asked here.对于其他 exercism.io 用户,请注意我遗漏了测试 4的评论:计算单元以正确的顺序获取输入,因为它与此处提出的问题不太相关。
Edit: Personally, I think the wording of that specific test 5 case is vague and confusing.编辑:就个人而言,我认为特定测试 5 案例的措辞含糊不清且令人困惑。
"compute cells update value when inputs are changed"
“当输入改变时计算单元更新值”
Which inputs and where?哪些输入和在哪里? It sounds like its asking you to some kind of event listener so that the values inside
InputCell
automatically trigger the values inside ComputeCell
to change -- bc I thought the same.这听起来像是要求您使用某种事件侦听器,以便
InputCell
内的值自动触发ComputeCell
内的值发生更改 - bc 我也这么认为。
But looking at the test, I think what they're actually asking for is: if you initialize a new instance of InputCell
with a number and pass that instance as an argument to create a new instance of ComputeCell
then your new ComputeCell
instance will have updated its value
(through the getter) based on the value
of the InputCell
instance you passed to it.但是看看测试,我认为他们实际上要求的是:如果您用一个数字初始化
InputCell
的新实例并将该实例作为参数传递以创建ComputeCell
的新实例,那么您的新ComputeCell
实例将已更新它的value
(通过 getter)基于您传递给它的InputCell
实例的value
。 It might have been clearer if the test message logged something this:如果测试消息记录了以下内容,则可能会更清楚:
"compute cells return different value when called with new inputs"
“当用新输入调用时,计算单元返回不同的值”
still probably not as clear as I'd like, but less misleading imo.仍然可能没有我想要的那么清楚,但不那么误导海事组织。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.