1

How can you invoke a function in JavaScript, while passing in arguments, using a function pointer?

Example:

function foo (a, callback) {    
        jQuery.post('/soon/check.json', { var:a }, function(resp) {
             callback(resp);
    }); 
}

function process_json(resp) {
  // Do something with resp
}

foo(bar, process_json);

process_json never gets invoked. Looking in Firebug, the string process_json is getting passed into foo, but I assumed this represents a pointer to the function process_json.

In Javascript, is it not possible to invoke functions via pointers and pass in arguments?

6
  • 1
    There's no reason to wrap callback in an anonymous function. $.post('/soon/check.json', {var : a}, callback) should work just as well. Commented May 16, 2011 at 19:58
  • 4
    Are you sure the ajax call is successful? If there was an error then your callback won't be hit. Commented May 16, 2011 at 19:59
  • jQuery.post's callback takes three params, the second of which is a text message describing the status of the request. Making AJAX calls without checking that the request succeeded and the result is valid is very bad programming practice. Commented May 16, 2011 at 20:08
  • @Rob the callback to $.post() is only invoked on success. Commented May 16, 2011 at 20:12
  • blush Ouch, you're right. So, OP would need to refactor to consider events. Commented May 16, 2011 at 20:15

4 Answers 4

6

In Javascript, is it not possible to invoke functions via pointers and pass in arguments?

It most certainly is possible to do this. Everything about your code looks just fine to me. Are you sure that the $.post() callback (the anonymous function) is being called? Is bar undefined when foo is invoked?

To clarify, we need to invoke a function using a string -- not a function pointer. Is this possible?

Yes. If the function is defined globally, you can invoke it as a property on the window object, like so:

function foo () { /* snip */ }

var fn_name = 'foo';

window.foo();       // works
window['foo']();    // works
window[fn_name]();  // also works
Sign up to request clarification or add additional context in comments.

4 Comments

Bar is defined. Yes, jQuery.post() is being called. This bug confuses us, too. The only other thing not mentioned is foo and process_json are in different javascript files, but process_json is available to foo (a javascript file gets included in the page where foo lives, and process_json is where this file lives).
@Crash: does the actual $.post pass { var:a }? That will definitely break things because var is a keyword.
Sorry, I'm an idiot. We were passing a string instead of the function pointer ... I would delete this question because it's too stupid to remain on SO, but I feel like I should award points to someone. Sorry for the waste of time, haha.
To clarify, we need to invoke a function using a string -- not a function pointer. Is this possible?
0
var process_json = function(resp) {
  // Do something with resp
}

Comments

0

Try this: http://jsfiddle.net/26naf/

Function alert is passed to foo by reference:

function foo(fref)
{
   fref("hi world");
}

foo(alert);

And it works as you see.

12 Comments

lol yes, but not in the same way. and the OP is passing a string "alert" not the literal alert
c-smile is right. We're passing in a function pointer, not a string. But for some reason it appears like a string in Firebug (though maybe we didn't look carefully at Firebug). We are definitely not (deliberately) passing in a string. Just the function pointer.
@Neal no, I don't believe the OP is passing a string.
@MattBall - i quote: " the string "process_json" is getting passed into "foo,""
Sorry, I'm an idiot. We were passing a string instead of the function pointer ... I would delete this question because it's too stupid to remain on SO, but I feel like I should award points to someone. Sorry for the waste of time, haha.
|
0

Of course you can pass in functions as callbacks, in fact by doing

jQuery.post('/soon/check.json', { var:a }, function(resp) {...

You are passing a callback that will be called after the post.

So the problem is somewhere else. Is the anonymous function passed to $.post really called ?

Hope this will help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.