0

What's the right way to use the arguments property of a function? This is how I'm currently using it, but I'm pretty sure I'm not using it correctly:

First, I define my parameters:

parameters = {};
parameters.ID = $tr.data('ID');
parameters.Name = 'Name goes here';
parameters.td = $td;
UpdateName(parameters);

And in the function:

var UpdateName = function(){
    var local = {};
    local.ID = arguments[0].ID;
    local.Name = arguments[0].Name;
    local.td = arguments[0].td;

    local.jqXHR = $.ajax('Remote/Ajax.cfc', {
        data: {
            method:'UpdateName'
            ,returnformat:'json'
            ,ID:local.ID
            ,Name:local.Name
        }
    });
    local.jqXHR.success(function(result){
        if (result.MSG == '') {
            local.td.text(local.Name).addClass('success');
        } else {
            local.td.addClass('err');
        };
    });
    local.jqXHR.error(function(result){
        local.td.addClass('err');
    });
}
3
  • If the ajax call finished extremely quickly, the success and error functions might not be defined. Better to define those as part of the settings object passed to $.ajax. Commented Mar 14, 2011 at 18:54
  • what reason do you have for calling a function in this way? Commented Mar 14, 2011 at 18:55
  • I'm trying to figure out the syntax of JavaScript. Commented Mar 14, 2011 at 19:55

3 Answers 3

5

The arguments object is most useful for functions that accept an arbitrary/unknown number of arguments. The MDC gives this example for creating HTML lists of any length:

function list(type) {
  var result = "<" + type + "l>";

  // iterate through non-type arguments
  for (var i = 1; i < arguments.length; i++)
    result += "<li>" + arguments[i] + "</li>";

  result += "</" + type + "l>"; // end list

  return result;
}

...which can be used like this:

var listHTML = list("u", "One", "Two", "Three");
// listHTML is "<ul><li>One</li><li>Two</li><li>Three</li></ul>"

Because of arguments, we can pass any number of items to list and it just works. (Notice that even in this case the known parameter, type, is named and iteration of arguments begins on its second element.)

Your list of parameters is well-defined, so there's no reason not to simply name the parameters in your function declaration. Using arguments as you are is unnecessary and harder to read.

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

Comments

3

You technically are using it correctly, but I don't see the practical point in your code though. The arguments variable contains a array-like object of the arguments passed to a function. E.g.

function test() {
    alert(arguments[0]);
    alert(arguments[1]);
}
test("Hello", 123); // alerts Hello and 123

In your example, instead of an object, you can pass the properties as arguments and retrieve them from arguments.

Comments

1

JavaScript functions are capable of using named arguments.

In your case, this could simplify your code as follows:

var UpdateName = function(local) {
    $.ajax('Remote/Ajax.cfc', {
        data: {
            method:'UpdateName',
            returnformat:'json',
            ID:local.ID,
            Name:local.Name,
            success: function(result) {
                if (result.MSG == '') {
                    local.td.text(local.Name).addClass('success');
                } else {
                    local.td.addClass('err');
                }
            },
            error: function(result) {
                local.td.addClass('err');
            }
        }
    });
}

I modified the code so that you don't need to keep a reference to jqXHR anymore since you aren't doing anything with it anyway. I also moved the leading commas to the end of each line, but that is just a preference.

3 Comments

Thanks for cleaning up my code. I got into the habit of putting the commas first due to SQL Server doing that, but I will change my habits from now on when it comes to js code.
Really, what I would like to use is function(arguments) instead of functon(local), because that's similar to the other language that I'm used to, but the word 'arguments' is a keyword that I can't use.
You could technically use function(arguments) if you really wanted to, but I wouldn't recommend it. Although it has a special purpose, arguments doesn't appear to be a reserved word so you are allowed to redefine it as you see fit. See http://jsfiddle.net/yxWZS/ for an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.