以前從未見過.apply方法。有人可以向我解釋它的作用嗎?這是從http://addyosmani.github.com/backbone-fundamentals/應用程序到底做了什麼?
var app = app || {};
var TodoList = Backbone.Collection.extend({
model: app.Todo,
localStorage: new Backbone.LocalStorage(’todos-backbone’),
completed: function() {
return this.filter(function(todo) {
return todo.get(’completed’);
});
},
remaining: function() {
return this.without.apply(this, this.completed());
},
nextOrder: function() {
if (!this.length) {
return 1;
}
return this.last().get(’order’) + 1; },
comparator: function(todo) {
return todo.get(’order’);
}
});
app.Todos = new TodoList();
看看這裏:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply – Blender
它可以讓你改變'this'的上下文並且傳遞一個數組作爲參數。 – elclanrs