0

Here is the descrition of getJSON in jquery:

$(selector).getJSON(url,data,success(data,status,xhr))

Here success(data,status,xhr) is a callback function when success. I often see the code below:

 $(select(.getJSON('my/url', function(data) {....});

Here variable data holds the data returned by the http server. My question is that instead of using name data here, can I use variable name like server_data as below?

 $(select(.getJSON('my/url', function(server_data) {....});
user938363
  • 9,990
  • 38
  • 137
  • 303

1 Answers1

2

It's a function argument name. You can name it whatever you want, so long as it follows the valid javascript variable name format.

Ref: What characters are valid for JavaScript variable names?

Edit: Also, easy enough to test without asking a question.

Taplar
  • 24,788
  • 4
  • 22
  • 35
  • `Taplar`, In callback function, how the server knows to load the return data in `data` variable if the variable name is arbitrary? I am confused with that. – user938363 Aug 30 '18 at 17:29
  • 2
    The server doesn't have any thing to do with what javascript does, in regards to variable names. The request returns data. jQuery passes the response into the callback method as the first argument. All it knows is that it passes it in as the first argument. Just like when you do a method like `Math.min(0, 1)`. Math may have given those two arguments names, but do you need to know them? No. You just need to pass in the expected data as the expected argument. – Taplar Aug 30 '18 at 17:32
  • 1
    @user938363: JavaScript uses positional function arguments. The function **caller** doesn't have to know the parameter names, that's an implementation detail of the function. What matters is that the arguments are passed in the correct order. When you call `getJSON`, then you also don't know how `getJSON` is defined and what its parameter names are. But you have to know in which order to pass the arguments. – Felix Kling Aug 30 '18 at 17:35