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;
};
This is the beginning of the code used in “Javascript: The Definitive Guide” to create a ‘Set’ Class. I’ve tried to rationalize the necessity of the apply() function in the constructor but can not figure it out for the life of me.
this.add.apply(this, arguments);
if the add() function is already a method called by ‘this‘ then what purpose or use is the core apply() function fulfilling. Thanks in advance to anyone who attempts to explain this to me
http://jsfiddle.net/Marlin/Ydwgv/ – Full Example from Javascript: The Definitive Guide
So that the
argumentscan be passed on as an entire collection. Useful ifSetaccepts an indefinite number of arguments.