最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - this.initialize(arguments) vs this.initialize.apply(this, arguments): what's the difference? - Stack Overfl

programmeradmin1浏览0评论

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().

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论