This.initialize (arguments) vs this.initialize.apply (this, arguments): what's the difference?

If you look at the source code of Backbone.js, you will see several uses of this template:

  this.initialize.apply(this, arguments);

For example, here:

  var Router = Backbone.Router = function(options) {
    options || (options = {});
    if (options.routes) this.routes = options.routes;
    this._bindRoutes();
    this.initialize.apply(this, arguments);
  };

Why just write this.initialize(arguments)instead?

+5
source share
1 answer
this.initialize.apply(this, arguments)

It works as follows:

this.initialize(arguments[0], arguments[1], arguments[2], ...)

Each element in the arguments is passed as a parameter initialize()

This is very different from just:

this.initialize(arguments)

Pass argumentsas the first and only parameterinitialize()

In other words, if the function expects the array to be the first parameter, use this.initialize(arguments), otherwise use .apply().

+8
source

All Articles