JavaScript "arguments" from John Resig number 40
<p>This is example  #40 from John Resigs Learning Advanced JavaScript <a href="http://ejohn.org/apps/learn/#40" rel="nofollow">http://ejohn.org/apps/learn/#40</a>.  It is called Using a <code>Variable Number of Arguments To Our Advantage.</code> </p>

<p>I have a couple questions if you are able to help (thank you)</p>

<p>1) in the 4th line of this function <code>root[key] = arguments[i][key];</code> are "root" and "arguments" exactly the same, because "arguments" is a reserved word that contains all the parameters passed to the function and "root" was the name given to the parameter? </p>

<p>2)<code>root[key] = arguments[i][key];  What exactly is the code doing in this line? Since "root" and "arguments" are already the same (as I guess above), isn</code>t this line superfluous? IsN`t it just saying that the properties of var merge are the same i.e. "name" = "name" and "city" = "city"?</p>

<p>3) In the second assert, he says "the city has been copied over". Could he have also said, for the first assert, "the name has been copied over"? ie. the same thing happens in the function to the "name" as the "city", correct?   Or to put it another way, he could have said in both asserts "the original name is intact" "the original city is intact"--namely, that they both underwent the same process?</p>

<p>4) Looking at the title of this example, "Using a variable number of arguments to our advantage," how does this example show that we can use a variable number of arguments to our advantage? Is it simply the fact that all the arguments get stored in "arguments" and can then be traversed as an array?</p>

<pre><code>function merge(root){ 
  for ( var i = 0; i &lt; arguments.length; i++ ) 
    for ( var key in arguments[i] ) 
      root[key] = arguments[i][key]; 
  return root; 
} 

var merged = merge({name: "John"}, {city: "Boston"}); 
assert( merged.name == "John", "The original name is intact." ); 
assert( merged.city == "Boston", "And the city has been copied over." );
</code></pre>
