this.initialize(arguments)vs this.initialize.apply(this,arguments):有什么区别?

The*_*mer 5 javascript backbone.js

如果您查看Backbone.js的源代码,您会看到此模式的多种用法:

  this.initialize.apply(this, arguments);
Run Code Online (Sandbox Code Playgroud)

例如,这里:

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

为什么不写this.initialize(arguments)呢?

Ja͢*_*͢ck 8

this.initialize.apply(this, arguments)
Run Code Online (Sandbox Code Playgroud)

像这样工作:

this.initialize(arguments[0], arguments[1], arguments[2], ...)
Run Code Online (Sandbox Code Playgroud)

参数中的每个项都作为参数传递给 initialize()

这与以下内容非常不同:

this.initialize(arguments)
Run Code Online (Sandbox Code Playgroud)

通过arguments作为第一个和唯一参数initialize()

换句话说,如果函数需要数组作为第一个参数,请使用this.initialize(arguments),否则使用.apply().