0

I have edited this question to clarify that this question does not relate to how to return a value from an ajax function call. It is asking why, when only 2 arguments are passed to $getJSON, the 2nd argument appears to be treated as though it were the 3ed. Having said that, please consider the following snippet of code:

<script>
 var fName;
 $.getJSON("work1.json", json);
 function json(data)
 {
  fName = data['fname'];
 }
</script>

Note that the $.getJASON function (which has one mandatory and 2 optional parameters) is given only 2 arguments. The first parameter is the mandatory "url" parameter. It is my understanding that the 2nd parameter is for data sent to the server and the 3ed parameter specifies the function to run if the request succeeds. Since only 2 arguments are passed, I would think that they would be for the 1st and 2nd parameters respectively. The first one is clearly the url. The 2nd one is behaving as though it were the 3ed parameter. Why is that? I thought that arguments passed to a function were assigned to the function's parameters in sequential order until exhausted. What am I missing?

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
user3311045
  • 689
  • 1
  • 7
  • 15
  • You are missing the fact that jQuery looks at arguments type. If second argument is `PlainObject` (as mentioned in [docs](http://api.jquery.com/jquery.getjson/)), then it will be treated as data. If it is `Function`, then it is success callback function. – Regent Oct 10 '14 at 05:13
  • If this question is not about how to return the value from an ajax call, your code shouldn't show the classic attempt to do so… – Bergi Oct 10 '14 at 05:22

1 Answers1

0

I thought that arguments passed to a function were assigned to the function's parameters in sequential order until exhausted.

Yes, they are. However, jQuery checks whether the second argument is a function, and uses that as the callback in case

getJSON: function( url, data, callback ) {
    return jQuery.get( url, data, callback, "json" );
},
get: function( url, data, callback, type ) {
    // Shift arguments if data argument was omitted
    if ( jQuery.isFunction( data ) ) {
        type = type || callback;
        callback = data;
        data = undefined;
    }
    return jQuery.ajax({
        url: url,
        type: method,
        dataType: type,
        data: data,
        success: callback
    });
}

(from https://github.com/jquery/jquery/blob/master/src/ajax.js#L766)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • That's very revealing. Thanks so much. Is there a cite where I can get more info? I'm interested in knowing whether this technique is pecular to getJSON or if it is used in other jquery functions. If it's used in other places, I'm interested in knowing how to tell which functions have it. Thanks again. – user3311045 Oct 10 '14 at 16:56
  • Yes, this kind of checking argument values is done all over jQuery's code. Just look at the [docs](http://api.jquery.com/), whenever there are any optional parameters (or even completely [overloaded methods](http://stackoverflow.com/q/12694588/1048572) that do something different, e.g. `.prop(set, value)` vs `.prop(get)`) this technique is used. Of course, these "optional arguments in the middle" is rather rare. – Bergi Oct 11 '14 at 15:38