This is lesson number 40 in John Resig's Advanced JavaScript course, titled "Leveraging Variable Number of Arguments." You can find the full content and explanation at http://ejohn.org/apps/learn/#40.
I have a few questions that I need help with:
1) Does the variable "root" in the function hold the same value as the reserved word "arguments"? Given that "arguments" contains all parameters passed to the function and "root" seems to be a parameter name.
2) Regarding the line of code root[key] = arguments[i][key]
, how does this line operate? Isn't it redundant since "root" and "arguments" appear synonymous – both reflecting properties like "name" equating to "name" and "city" equating to "city"?
3) The author mentions "the city has been copied over" in one assert. Could they have also mentioned "the name has been copied over" in the first assert? Essentially implying that the process applied to "name" mirrors that of "city."
4) Exploring the example's title, "Using a variable number of arguments to our advantage," how exactly does this example demonstrate the benefit of handling variable arguments? Is it solely about storing all arguments in "arguments" for easy array traversal?
function merge(root){
for ( var i = 0; i < 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." );