Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone example app and javascript apply

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]
like image 375
andrei Avatar asked Jul 19 '13 19:07

andrei


People also ask

Is Backbone framework of JavaScript?

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.

What is backbone JS used for?

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.

Is Backbone JS frontend or backend?

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.


2 Answers

Variable list of arguments

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.

like image 153
AD7six Avatar answered Oct 29 '22 05:10

AD7six


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.

like image 30
Michael Geary Avatar answered Oct 29 '22 06:10

Michael Geary