2

I am having a hard time understanding the arguments object. In this code, what function would arguments be looking at?

getAjax('get_info', 'java_array', realVarName, cld.listArray, 0, '',           
'no_restrict', function() {
 show_list(arguments[0], sld);

    if (typeof(postFunc) == "function") {
        postFunc();
    }
 });
1
  • Did one of the answers below answer your question? If so, please select the best answer and click the green checkmark to the left of it. This will indicate to the community that your question has been answered and will earn both you and the answerer some reputation points. Commented Jan 7, 2016 at 10:24

3 Answers 3

3

'arguments' is an inherit variable with any function. It contains all the parameters passed to a function. For instance, a function definition may not list any parameters, however an invocation could include 'n' parameters on it. The function could then still access all of them through the arguments array.

Sign up to request clarification or add additional context in comments.

Comments

1

The arguments object is an Array-like object corresponding to the arguments passed to a function.

In your code the arguments[0] will primarily be undefined

1 Comment

You have no idea whether arguments[0] will be undefined or not. It depends entirely upon what getAjax() passed to the callback when it calls it. If it passes arguments, then arguments[0] will certainly have a value.
0

First off, arguments is an object that represents the set of arguments passed to the current function. It exists automatically inside of every function. You can read more about it here on MDN.

So, in your case, arguments[0] will be the first argument that getAjax() passes to the callback you passed into it as the last argument when you called getAjax(). So, it depends upon the internal behavior of getAjax() which you do not show us.

Let's walk through how your code works:

  1. You make a function call to getAjax() and pass it a number of arguments.
  2. One of those arguments is a callback function (the last argument).
  3. When getAjax() is doing its job, it will, at some point, call that callback function.
  4. When it calls that callback function, it can pass that callback some arguments.
  5. Within that callback function the arguments object will represent whatever arguments were passed to it by getAjax().
  6. So, when (in that callback function), you then get arguments[0] and pass it to show_list(), you will be passing whatever the first argument was that getAjax() passed to your callback.

You could rewrite your code without using the arguments object like this by just declaring a named argument for your callback:

getAjax('get_info', 'java_array', realVarName, cld.listArray, 0, '',  
  'no_restrict', function(obj) {

    show_list(obj, sld);

    if (typeof(postFunc) == "function") {
        postFunc();
    }
 });

In this alternate implementation, you name the first argument to the callback function to be obj and use it directly rather than using arugments[0]. Both your implementation and this produce the same result.


It is generally better to use named arguments (like the obj in my alternate implementation) when you know what arguments are going to be passed to a function as this makes the code more self documenting. But the arguments object can be particularly useful when you don't know how many arguments are going to be passed to a function or when you want to pass whatever arguments were passed to your function to some other function (forwarding or proxying).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.