Backbone.js 主干js Todo应用程序_下划线.js

Backbone.js 主干js Todo应用程序_下划线.js,backbone.js,underscore.js,Backbone.js,Underscore.js,我很难理解下面代码中剩余的函数是如何工作的 (注:来源:) 我对apply的理解是,第一个参数是上下文,其余参数是作为参数传递给正在应用的函数的数组 var TodoList = Backbone.Collection.extend({ model: Todo, localStorage: new Backbone.LocalStorage("todos-backbone"), done: function() { return t

我很难理解下面代码中剩余的函数是如何工作的

(注:来源:)

我对apply的理解是,第一个参数是上下文,其余参数是作为参数传递给正在应用的函数的数组

var TodoList = Backbone.Collection.extend({
      model: Todo,
      localStorage: new Backbone.LocalStorage("todos-backbone"),
      done: function()
      {
        return this.filter(function(todo) { return todo.get('done'); });
      },
      remaining: function() 
      {
        return this.without.apply(this, this.done());
      },
});
因此:

this.without.apply(this,this.done());-->翻译为:

without(array of arguments as parameters to without function);
Wither将第一个参数作为数组,并从数组中删除2…n个参数

我不理解这个函数是如何做任何有用的事情的。解释一下我遗漏了什么会有所帮助

this.without.apply(this,this.done());-->转换为:
不带(数组)

不是真的。您知道的以数组作为第一个参数的下划线函数可以作为主干集合上的方法应用(如下划线链接包装器)。让我们假设
this.done()
的计算结果为
[x,y,z]
,然后调用转换为

this.without(x, y, z);
当然,更有效的方法是

return this.filter(function(todo) { return ! todo.get('done'); });
//                                         ^

主干:将每个下划线方法混合作为集合#模型的代理


但是,如果this.done()返回一个数组,我仍然不理解它如何过滤掉未完成的项。我理解你的例子。过滤器,但有一块我必须错过。你是说this.without.apply是一个下划线方法,它应用如下内容:this.without(当前的\u集合,已完成的\u元素的数组?)
apply
不是下划线方法,但是函数对象的本机方法-请参阅我上面链接的文档。我理解应用的方式如下:第一个参数是它的上下文,第二个参数是一个数组,其中包含函数对象的1…n个参数。返回this.without.apply(this,this.done())似乎没有意义,因为this.done()返回已完成的对象数组。我看不出它是如何过滤掉那些不是doneYes的项目的,没错。现在,整个集合(
这个
)减去(
没有
)那些
完成的(
剩余的
那些(没有完成)…啊,明白了。所以“这”是收藏,我想这是让我困惑的。
_.each(methods, function(method) {
    Collection.prototype[method] = function() {
      //convert arguments to array. Such as: args = [1, 2, 3]
      var args = slice.call(arguments);
      //add this.models at the begining of array. Such as: [['string'], 1, 2]
      args.unshift(this.models);
      //_[without].apply(_, [['string'], 1, 2])
      //_.without(['string'], 1, 2);
      return _[method].apply(_, args);
    };
  });