Hi can someone explain why in backbone example app (http://backbonejs.org/examples/todos/index.html) in remaining() function, is called using apply (this.without.apply(this, this.done());) and not this.without(this.done())
// Filter down the list of all todo items that are finished.
done: function() {
return this.where({done: true});
},
// Filter down the list to only todo items that are still not finished.
remaining: function() {
return this.without.apply(this, this.done());
},
Thank You !
#Update
Debugger output
this.without(this.done())
[child, child, child, child]
this.without.apply(this, this.done());
[child, child, child]
js is a JavaScript rich-client web app framework based on the model–view–controller design paradigm, intended to connect to an API over a RESTful JSON interface. Backbone is known for being lightweight, as its only hard dependency is on one JavaScript library, Underscore.
BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.
js. Backbone. js developers usually focus on the front-end aspect of web applications, but also must have basic understanding of some back-end technologies, since their responsibility includes the integration of APIs and resources with front-end elements as provided by the back-end developers and engineers.
The key is in the way without is written:
function () {
var args = slice.call(arguments);
args.unshift(this.models);
return _[method].apply(_, args);
}
It's anticipating a variable list of arguments, and one way to do that is to use apply:
...
return this.without.apply(this, ['pass', 'these', 'arguments']);
There's more about apply in the MDN documentation.
You asked what is the difference between these two calls:
this.without( this.done() )
vs.
this.without.apply( this, this.done() );
To clarify, let's remove the nested this.done()
call. Now the first one is:
var value = this.done();
this.without( value );
That code obviously calls this.without()
and passes it a single argument, whatever value
was returned by this.done()
. If value
happens to be an array, the entire array is passed as a single argument.
The second version becomes:
var array = this.done();
this.without.apply( this, array );
That calls this.without()
with a variable number of arguments, one argument for each element of array
. (And I called it array
instead of value
this time, because for this code to make sense it has to be an array.)
.apply()
also sets this
in the called function, so passing this
as the first argument just passes this
along to that function in the same manner as a regular this.without()
method call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With