tags:

views:

45

answers:

2

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

A: 

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.

d11wtq
and i get: method is not a function
Totty
+5  A: 

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')
Matt Ball