在Backbone Todo MVC源代码中,该函数的本机apply方法用于在不使用的情况下调用Underscore方法,而且我不明白为什么这样做是必要的。
// Filter down the list of all todo items that are finished.
completed: function() {
return this.filter(function( todo ) {
return todo.get('completed');
});
},
// Filter down the list to only todo items that are still not finished.
remaining: function() {
return this.without.apply( this, this.completed() );
},
要将呼叫没有看起来不合适,相比于其他下划线的方法,如过滤器。我仔细检查了Backbone来源,以确保没有将No混入Collection对象中。果然不是。
下划线方法是如何附加到Collection的:
_.each(methods, function(method) {
Collection.prototype[method] = function() {
var args = slice.call(arguments);
args.unshift(this.models);
return _[method].apply(_, args);
};
});
如预期的那样-Collection的模型已经作为第一个参数传递了。此外,由于方法是在Collection对象上调用的,因此将正确地对其进行限制。
我通过将方法更改为以下内容来验证了这一点
this.without(this.completed());
那很好。
我在这里俯瞰什么?
我认为您没有忽略任何事情。这只是对的不必要调用apply
。可能作者最初写了以下内容(可能是主干的早期版本)。
// Filter down the list to only todo items that are still not finished.
remaining: function() {
return _.without.apply( this, this.completed() );
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句