0

I have:

var f1 = function(a){
    alert(a)
}

var f2 = function(data, method){
    method(data) // the problem is here, 
    // the method f1 is not called. Is there a way to call that method f1?
    // the method f1 might not be in this scope, the method f1 can 
    // be in a class or like this... 
}

f2(a, f1)

The question is: Is there a way to call that f1 from f2, from the passed method? thanks

EDIT: this is some code I write here, but I miss to set the a. anyway the value of is 5. EDIT: yes! it was just a tiny stupid error in my original code that missed up, i set the value after calling the method. hehe

4
  • 1
    What's a? A quick test in firebug shows it works. Commented Apr 27, 2010 at 14:59
  • it won't do anything because 'a' is not defined. Otherwise it works. Commented Apr 27, 2010 at 15:02
  • of course, a has some value *** Commented Apr 27, 2010 at 15:04
  • now it works, thanks. This code was just an example of the actual code, so the value of a doesnt matter Commented Apr 27, 2010 at 15:10

2 Answers 2

5

Try running your Javascript through a debugger. You'll get a message like a is not defined because the call f2(a, f1) is trying to pass a variable named a but you haven't declared one. However, this code will work:

var f1 = function(a){
    alert(a);
}

var f2 = function(data, method){
    method(data);
}

var a = 'this is a';
f2(a, f1); // results in alert('this is a')
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, this should work fine. In your example however, a is undefined when you make the invocation. Make sure you define it; this could be the reason it's not working.

In other words, changing your example to:

f2(42, f1);

Will alert the number 42.

1 Comment

and i get: method is not a function

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.