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的完整示例
javascript ×1