mjmitche
mjmitche

Reputation: 2067

JavaScript "arguments" from John Resig number 40

This is example #40 from John Resigs Learning Advanced JavaScript http://ejohn.org/apps/learn/#40. It is called Using a Variable Number of Arguments To Our Advantage.

I have a couple questions if you are able to help (thank you)

1) in the 4th line of this function root[key] = arguments[i][key]; 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?

2)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), isnt this line superfluous? IsN`t it just saying that the properties of var merge are the same i.e. "name" = "name" and "city" = "city"?

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?

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?

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." );

Upvotes: 2

Views: 436

Answers (2)

Quentin
Quentin

Reputation: 943578

1) in the 4th line of this function root[key] = arguments[i][key]; are "root" and "arguments" exactly the same

No. root is the same as arguments[0], not arguments

2)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), isnt this line superfluous?

Only when i is 0. The function would be slightly more efficient if it started at 1

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"?

Not really, since it was being assigned to itself, so there is still only one instance of it.

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?

It might be perhaps better titled "Using JavaScript's capability to handle variable and arbitrary numbers of arguments to a function to our advantage", but that would be rather long winded.

Upvotes: 4

Rob Agar
Rob Agar

Reputation: 12459

He's exploiting the fact that in Javascript you can pass more arguments than are declared in the function definition. These extra arguments are accessible via the arguments array passed to all functions.

In this case root is the named first argument, but there could be more unnamed ones.

Here he is passing two arguments:

var merged = merge({name: "John"}, {city: "Boston"}); 

So root is the object {name: "John"}, but arguments is an array containing both objects

The answer to #4 is basically yes.

Upvotes: 2

Related Questions