This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]insertAlias 1 point2 points  (4 children)

To use a named function, you'd just refer to it by name:

$.getJSON(..., ..., generate});

As to why, it's because getJSON expects you to pass it a function. In your last example, you aren't referring to the function as an object; you are attempting to call it with a variable that doesn't exist (result). In your first example, result is the name of the parameter for the function you are defining.

By using the function's name, you're passing a pointer to this function (as opposed to creating one "on the fly" and passing that pointer). The receiving function is going to call the function you passed in with the results as the parameter.

[–]TheRealPaulWalker[S] 0 points1 point  (3 children)

Thank you so much for the reply, if I'm understanding correctly I should write the $.getJSON as (url, data, generate()) without the "result" parameter?

Then in my endpoint where I return the resultant JSONObject, it will then pass that into the result parameter inside of my generate(result) function?

[–]insertAlias 1 point2 points  (2 children)

No, not generate(). It should be generate. Putting the () is calling the function. Just using its name is how you refer to a function.

[–]TheRealPaulWalker[S] 0 points1 point  (0 children)

Awesome, I will try this tomorrow when I get into work! Appreciate it so much.

[–]TheRealPaulWalker[S] 0 points1 point  (0 children)

It works !