王存露
王存露

Reputation: 11

Javascript function

This is my first question at stackoverflow, i hope someone can help me

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)
    },

Why can this "filters [this.visibility](this.todos)" use I used to be alert () so Not used alert [] () like this please help me

Upvotes: -1

Views: 83

Answers (1)

Barmar
Barmar

Reputation: 780974

filters[this.visibility](this.todos) means that filters[this.visibility] evaluates to a function.

E.g. if this.visibility = "all" then filters[this.visibility] means filters["all"]. You then call that function with the argument this.todos. It's equivalent to writing

filters.all(this.todos)

but it allows the function to be selected dynamically based on this.visibility.

Upvotes: 1

Related Questions