How do you formulate a JavaScript function call from within JS

Hello,

We have a JS function that we need to call which is like:

function load_comments (qid, comm_div, anim_div, start_id, op_owner) {

}

we need to call this when we come back from AJAX with values brought back by AJAX, that is like this:

load_comments('\' + results.id_qa + '\', \'' + results.post_id + '\', \'' + results.div_anim + '\', \'' + results.start_id + '\', \'' + cur_user + '\');

but it is not working!
What do we need to do to correct above function call?

Thanks.

Hi there WorldNews,

try it like this,

load_comments( results.id_qa + ',' + results.post_id + ',' + results.div_anim + ',' + results.start_id + ',' + cur_user  );

< offtopic >

in this thread of yours…

https://www.sitepoint.com/community/t/what-is-css-for-this-super-thick-line/307170

…you asked for a particular font.

Do you still require it?

< / offtopic >

coothead

Hi Coot,

That is not working.
As all the arguments called by load_comments function are assigned to the 1st argument q_id
and the other arguments are left undefined. Producing this result from console.logs:

q_id = 136844,here_comm_136844,id_anim_136844,start_id_136844,2486805

comm_id = undefined
anim_id = undefined
count_id = undefined

Also keep in mind that some of these variable/arguments are string and not integer.

And yes I am still interested in that Super BIG font too. We can talk about it after fixing this critical issue.

Thanks.

Make the function accept an object as an argument:

function load_comments (obj) {

}

Then call it like so:

load_comments({
  foo: "bar"
});

And whatever you pass it will be available on the object it receives as an argument:

function load_comments (obj) {
  console.log(obj.foo);
  // bar
}
2 Likes

For various reasons we need to stay with function call as it is.

You have a solution to how pass its arguments values as is?

Nope. I would change it.

This:

load_comments('\' + results.id_qa + '\', \'' + results.post_id + '\', \'' + results.div_anim + '\', \'' + results.start_id + '\', \'' + cur_user + '\');

is pretty unreadable and, as you are discovering, hard to maintain.

Although, saying that, I guess you could look into template strings to tidy it up a little.

Good luck.

Have you considered using the apply method, that lets you use an array of values as function arguments?

That apply() method got the Job done :slight_smile:
Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.