admin管理员组

文章数量:1321425

If you have a look at Backbone.js's source code, you'll see multiple uses of this pattern:

  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 not just write this.initialize(arguments) instead?

If you have a look at Backbone.js's source code, you'll see multiple uses of this pattern:

  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 not just write this.initialize(arguments) instead?

Share Improve this question edited Mar 3, 2013 at 6:56 TheFooProgrammer asked Mar 3, 2013 at 6:51 TheFooProgrammerTheFooProgrammer 2,5275 gold badges31 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8
this.initialize.apply(this, arguments)

Works like this:

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

Each item in arguments is passed as a parameter to initialize()

Which is very different from just:

this.initialize(arguments)

Pass arguments as the first and only parameter to initialize()

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

本文标签: