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
1 Answer 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.
answered May 6, 2017 at 14:08
Barmar
789k57 gold badges555 silver badges669 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
王存露
Thank you for answering me, I want to understand
lang-js
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