2017-05-06 17 views
0

これはstackoverflowの時の私の最初の質問ですが、私は誰かが私Javascriptの機能

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

なぜこの「フィルター[this.visibility](this.todos)は」 を使用することができ、私がために使用を助けることを願って警告()はそう、この like 未使用の警告は、[]()私は

+1

あなたの質問は明確ではありません - 正確に何を求めていますか? – bouteillebleu

+0

'filters [this.visibility]'は関数呼び出しではなく、[Bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/)を使ってアクセスするプロパティです。 Property_Accessors#Bracket_notation) - 結果のプロパティ_は関数なので、最後に '(this.todos)'を呼び出す – Matt

+0

* "どうすれば" [this.visibility](this.todos) )そうではない[] {}()。 "*英語お願いします。 – zer00ne

答えて

1

filters[this.visibility](this.todos)filters[this.visibility]は、関数に評価されることを意味助けてください。

など。 this.visibility = "all"ならばfilters[this.visibility]filters["all"]を意味する。その関数を引数this.todosで呼び出します。それは

filters.all(this.todos) 

を書くと同等だが、それは関数がthis.visibilityに基づいて動的に選択することができます。

+0

あなたは私に答えるために、私は理解したい –