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?
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?
Share Improve this question edited May 21, 2022 at 8:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 16, 2011 at 19:53 CrashalotCrashalot 34.6k63 gold badges284 silver badges460 bronze badges 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. – Tikhon Jelvis 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. – onteria_ 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. – Rob Raisch Commented May 16, 2011 at 20:08
-
@Rob the callback to
$.post()
is only invoked on success. – Matt Ball Commented May 16, 2011 at 20:12 - blush Ouch, you're right. So, OP would need to refactor to consider events. – Rob Raisch Commented May 16, 2011 at 20:15
4 Answers
Reset to default 6In 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
var process_json = function(resp) {
// Do something with resp
}
Try this: http://jsfiddle/26naf/
Function alert
is passed to foo by reference:
function foo(fref)
{
fref("hi world");
}
foo(alert);
And it works as you see.
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