-1

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

6
  • 1
    Your question isn't quite clear - what exactly are you asking? Commented May 6, 2017 at 13:43
  • filters[this.visibility] is not a function call, it's a property being accessed using Bracket notation - the resulting property is a function, hence the (this.todos) invocation at the end Commented May 6, 2017 at 13:44
  • "How do that "filters[this.visibility] (this.todos) use I used to be alert() so NOT []{}(). " English please. Commented May 6, 2017 at 14:06
  • @zer00ne take it easy, obviously OP is a non-native speaker... Commented May 6, 2017 at 14:11
  • Reading the first sentence indicates OP has a grasp of the English language. @ironicaldiction I said please, I did not vote to close this question, nor have I downvoted it, so what else do you think I could do to "take it easy?" Should I edit the last sentence for OP,? I would gladly do it if what was written had any coherence. Commented May 6, 2017 at 14:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answering me, I want to understand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.