Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  103] [ 3]  / answers: 1 / hits: 19220  / 12 Years ago, fri, november 30, 2012, 12:00:00

Not sure if the title is worded correctly, or if there is a better way of saying it, but I think its okay.



At any rate, I understand the following thus far:



a.b(a, b, c, foo);


Where foo is a function defined elsewhere that takes in no arguments would simply result in the function a.b() running, with the above parameters. The parameter foo can then be called inside function a.b() as simply foo(). In other words, I understand the above call to be using a function pointer as an argument in the function a.b as a parameter.



Okay, now here is what I'm trying to do...



I want to be able to do something similar to the above, except this time I want foo to have a paramater passed in that argument as follows:



a.b(a, b, c, foo(bar));


Now here is the problem. This will literally result in the paramaters a, b, c and the result of foo(bar) being used. I don't want this. I want foo(bar) to literally be passed in so that in the function a.b which would look like this (as the header):



a.b(first, second, third, fourth);


Could reference and call the fourth parameter as:



fourth();


Even if fourth has an argument in it. I can't seem to find a way around this problem, any advice?



Thanks!


More From » function

 Answers
121

Use an anonymous function to wrap your foo call.



a.b(a, b, c, function() {return foo(bar);});





If you need to retain the this value that would be given, you can invoke it using .call. You can also pass along any arguments given.



a.b(a, b, c, function(arg1, arg2) {return foo.call(this, bar, arg1, arg2);});





And of course the function doesn't necessarily need to be anonymous. You can use a named function just as well.



function bar(arg1, arg2) {
return foo.call(this, bar, arg1, arg2);
}
a.b(a, b, c, bar);

[#81703] Thursday, November 29, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
faithemilys

Total Points: 418
Total Questions: 100
Total Answers: 114

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
;