tags:

views:

75

answers:

2

Hi,

yet again thanks for looking.

i like to call a function (eg. comment_disp, post_disp or any other i create later on).

i have created a json function with url, fname and id can i use fname as a function name?

// on document ready runs
json('comments.php','comment_disp');
  • url = url to get
  • fname = function
  • name id = post or any other data id.
json = function(url,fname,id){
$.getJSON(url,(fname));
}

comment_disp = function(json){
}
+2  A: 

Functions are first-class objects in JavaScript. You can put them into parameters directly, no need to transport their name as string.

So instead of

json = function(url,fname,id){ $.getJSON(url,(fname)); }

do

json = function(url,f,id){ $.getJSON(url, f); }

and call it as

json('comments.php', comment_disp);
Tomalak
There is nothing wrong with using `(fname)`.
Gumbo
thanks you very much.
jay
@Gumbo: I wanted to make clear that the function *itself*, not its *name* should be transported. The argument name of course makes no difference, it could (though logically wrong) be kept as `fname`.
Tomalak
+1  A: 

Assuming your functions are available in the "global scope" (i.e. not written as a private member of another class/constructor), you can call your functions by string name like this:

window['comment_disp']();

This would be the equivalent of calling

comment_disp();
Andrew