有一天我问了一个问题( here )。有人善意地回复了,他们的回答符合我最初的意图。但是,有一些代码我不明白,我希望有人可以帮助我。
self.currentlySelected = ko.computed(function() {
return self.topics().reduce(function(count, topic) {
return count + (topic.selected() ? 1 : 0);
}, 0);
});
有人能解释一下这个函数是做什么的吗?谢谢!
最佳答案
它表示 currentlySelected
是一个计算属性 - 要查看它的值,您必须评估一个函数。
该函数表示:
1. Get the topics
2. Reduce the topics down to a single value, using the following function
3. If the topic is selected, add 1 to our running total (count).
Otherwise, add 0. Our initial value for count is 0.
关于Javascript 函数解释 - Knockout.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38489789/