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;
};
这是“Javascript:The权威指南”中用于创建“Set”类的代码的开始。我试图在构造函数中合理化apply()
函数的必要性,但无法想象出我的生活。为什么构造函数中需要apply()函数
this.add.apply(this, arguments);
如果add()
功能已经是一个被“this
”又是什么目的或用途调用的方法是核心apply()
功能实现。在此先感谢任何人谁试图解释这个给我
http://jsfiddle.net/Marlin/Ydwgv/ - 从Javascript完整的例子:权威指南
它似乎没有什么用处。我有*从未*见过它...... – Blender
你对传递参数对象的事实有何想法? – ChaosPandion
你不认为它可能与确保add()不作为实例的属性而不是原型添加有关?我明白这不是正在宣布......但它肯定令我困惑 – Marlin