javascript - JS函数问题
黄舟
黄舟 2017-05-19 10:18:18
0
1
354
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)
},

很想问一下filteredTodos这个方法调用filters方法怎么有数组呢?这是什么用法呢?求解惑~

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
大家讲道理

filters[this.visibility] 这里不是数组,是调用对象下的方法。

this.visibility的结果可能有三个 all, active, completed 所以最终是个类似于这样的东西:

filters['all'] 就相当于调用了 filters 对象下的 all 方法,因为 this.visibility 是个变量,所以必须写成这种写法

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!