计数器

  • version: v0.1.1
  • desc: 计算 todos 中 未完成(completed=false) 的个数
  • codes:

    1
    2
    3
    scope.remaining = scope.todos.reduce(function (n, todo) {
    return n + (todo.completed ? 0 : 1)
    }, 0)
  • test:

    1
    2
    3
    4
    5
    6
    var todos = [{completed: true}, {completed: false}, {completed: true}, {completed: false}, {completed: true}];
    var remaining = todos.reduce(function (n, todo) {
    return n + (todo.completed ? 0 : 1)
    }, 0)
    console.log(remaining)
    // 2

过滤器

  • version: v0.1.1
  • desc: 返回一个匿名函数,内部对比keyCode和传入的code是否相等,相等则执行函数。
  • codes:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    var keyCodes = {
    enter: 13,
    tab: 9,
    'delete': 46,
    up: 38,
    left: 37,
    right: 39,
    down: 40
    }
    const key = function (handler, args) {
    var code = keyCodes[args[0]]
    if (!code) {
    code = parseInt(args[0], 10)
    }
    return function (e) {
    if (e.originalEvent.keyCode === code) {
    handler.call(this, e)
    }
    }
    }