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