为什么构造函数中需要apply()函数

Mar*_*lin 5 javascript

function Set() {          // This is the constructor
    this.values = {};     
    this.n = 0;          
    this.add.apply(this, arguments);  // All arguments are values to add
}

// Add each of the arguments to the set.
Set.prototype.add = function() {
    /* Code to add properties to the object's values property */
    return this;
};
Run Code Online (Sandbox Code Playgroud)

这是"Javascript:The Definitive Guide"中使用的代码的开头,用于创建"Set"类.我试图apply()在构造函数中合理化函数的必要性,但无法弄清楚我的生活.

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

如果add()函数已经是' this' 调用的方法,那么apply()实现的核心功能是什么目的或用途.提前感谢任何试图向我解释这一点的人

http://jsfiddle.net/Marlin/Ydwgv/ - Javascript:The Definitive Guide的完整示例

use*_*716 7

"如果add()函数已经是'this'调用的方法,则实现核心apply()函数的目的或用途是什么. "

这样arguments就可以作为整个集合传递.如果Set接受无限数量的参数,则很有用.