Secrets of the JavaScript Ninja explains the arguments keyword with the merge() function:
function merge(root){
for (var i = 1; i < arguments.length; i++) { // starts at i = 1, not 0
for (var key in arguments[i]) {
root[key] = arguments[i][key];
}
}
return root;
}
var merged = merge(
{name: "Batou"},
{city: "Niihama"});
Note the assertions:
assert(merged.name == "Batou",
"The original name is intact.");
assert(merged.city == "Niihama",
"And the city has been copied over.");
Why does merged.name equal Batou rather than undefined?
Since, as I understand, merge() does not look at the first argument in the outer for-loop, how does the name: Batou get added to root?
argumentsis not a keyword. It's a normal variable (name).root.nameis called first inroot [name] = Batou? I don't understandrootis the object that is passed as first argument tomerge, i.e.{name: "Batou"}. It already contains the propertyname.arguments, but themergefunction above seems confusing to me. Is it an acceptable/passes code review practice in JS?