Javascript 函数解释 - Knockout.js

标签 javascript jquery knockout.js

有一天我问了一个问题( 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/

相关文章:

相当于 Ruby Timeout 模块的 JavaScript/CoffeeScript

javascript - document.getElementById + 正则表达式

javascript - 如何在iframe中输出codemirror的结果?

jquery - 在打开页面之前预加载图像

knockout.js - MVC 4 knockout 列表

javascript - 在 IE 7 中强制重绘 Flash 内容

javascript - $ (“#request-brochure” ).validate 不是一个函数

javascript - 在 onclick 中定位所有已加载或尚未加载的 <a>

javascript - Durandal 2.0 - 更新 View 中可观察值的值

knockout.js - 使用knockout js将2个可观察数组链接在一起