Home  >  Q&A  >  body text

javascript - JS function problem

var filters = {
  all: function (todos) {
    return todos
  },
  active: function (todos) {
    return todos.filter(function (todo) {
      return !todo.completed
    })
  },
  completed: function (todos) {
    return todos.filter(function (todo) {
      return todo.completed
    })
  }
}

filteredTodos: function () {
  return filters[this.visibility](this.todos)
},

I would like to ask how the filteredTodos method calls the filters method to have an array? What is the usage? Please solve your doubts~

黄舟黄舟2734 days ago383

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-19 10:20:18

    filters[this.visibility] This is not an array, but a method under the calling object.

    this.visibility的结果可能有三个 all, active, completed So it ends up being something like this:

    filters['all'] 就相当于调用了 filters 对象下的 all 方法,因为 this.visibility is a variable, so it must be written like this

    reply
    0
  • Cancelreply