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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • 1
    There's no reason to wrap `callback` in an anonymous function. `$.post('/soon/check.json', {var : a}, callback)` should work just as well. – Tikhon Jelvis May 16 '11 at 19:58
  • 4
    Are you sure the ajax call is successful? If there was an error then your callback won't be hit. – onteria_ May 16 '11 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. – Rob Raisch May 16 '11 at 20:08
  • @Rob the callback to `$.post()` is only invoked on success. – Matt Ball May 16 '11 at 20:12
  • *blush* Ouch, you're right. So, OP would need to refactor to consider events. – Rob Raisch May 16 '11 at 20:15
  • Long story short, we do want to pass in a string instead of the function pointer ... is it possible to invoke the function just knowing its name (through the string)? – Crashalot May 16 '11 at 20:16

4 Answers4

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
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 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). – Crashalot May 16 '11 at 20:01
  • 1
    @Crash: does the actual `$.post` pass `{ var:a }`? That will ***definitely*** break things because `var` is a keyword. – Matt Ball May 16 '11 at 20:05
  • 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. – Crashalot May 16 '11 at 20:12
  • To clarify, we need to invoke a function using a string -- not a function pointer. Is this possible? – Crashalot May 16 '11 at 20:22
0
var process_json = function(resp) {
  // Do something with resp
}
Kasturi
  • 3,335
  • 3
  • 28
  • 42
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.

c-smile
  • 26,734
  • 7
  • 59
  • 86
  • lol yes, but not in the same way. and the OP is passing a string `"alert"` not the literal `alert` – Naftali May 16 '11 at 20:02
  • 2
    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. – Crashalot May 16 '11 at 20:03
  • @Neal no, I don't believe the OP is passing a string. – Matt Ball May 16 '11 at 20:03
  • @MattBall - i quote: `" the string "process_json" is getting passed into "foo,""` – Naftali May 16 '11 at 20:05
  • 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. – Crashalot May 16 '11 at 20:12
  • @Neal Long story short, we do want to pass in a string instead of the function pointer ... is it possible to invoke the function just knowing its name (through the string)? – Crashalot May 16 '11 at 20:16
  • @Neal, aren't you just passing in the function pointer to alert as opposed to the string "alert?" – Crashalot May 16 '11 at 20:19
  • To clarify, we need to invoke a function using a string -- not a function pointer. – Crashalot May 16 '11 at 20:22
  • @CrashAlot, nope thats a string, to see the function method look at the fiddle in this answer – Naftali May 16 '11 at 20:22
  • Found this, @Neal: http://stackoverflow.com/questions/912596/how-to-turn-a-string-into-a-javascript-function-call – Crashalot May 16 '11 at 20:25
  • @Neal, sorry about that! We clicked on c-smile's fiddle -- not yours. – Crashalot May 16 '11 at 20:30
  • @Neal ... hence all the confusion. Sorry. Need more caffeine this morning, haha. – Crashalot May 16 '11 at 20:30
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

sitifensys
  • 2,024
  • 16
  • 29