0

So I have these two javascript functions:

function some_name(say_something){
    console.log('hello' + say_something);
}

call_other_fuction_after_doing_something('some_function')


// In some other file, some where - in the land of make believe.
function call_other_fuction_after_doing_something(function_name){
    $.ajax({
        url : /*Go some where*/,
    type : 'POST'
    data : { /*Some kind of data*/},
    success : function(result){
        if(function_name !== undefined){
        $.fn[success_action](result); // Pay attention to this!
        }
    },
    });
} 

So as we can see I do some function in some file, some where that logs to the console: "hello" which then has what ever the result is that comes back from the ajax call appended to it.

I assumed this would work, as I read something about this here in this question. But apparently I was wrong as the error I get is:

Uncaught TypeError: Object [object Object] has no method 'some_name' 

Any ideas how to do this kind of "Reflection" in javascript (jquery)?

Community
  • 1
  • 1
TheWebs
  • 12,470
  • 30
  • 107
  • 211

1 Answers1

1

What you are trying to do is to call a function which is a property of $.fn, which it looks like does not exists.

Try passing the function reference instead of name

function some_name(say_something){
    console.log('hello' + say_something);
}

call_other_fuction_after_doing_something(some_name)


//In some other file, some where - in the land of make believe.
function call_other_fuction_after_doing_something(fn){
    $.ajax({
        url : /*Go some where*/,
    type : 'POST'
    data : { /*Some kind of data*/},
    success : function(result){
        if(typeof fn == 'function'){
            fn(result); // Pay attention to this!
        }
    },
    });
} 
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • The function in question, spelling mistakes aside, `some_name` is never called, if I do a console.log(fn) out side the if statement and inside I get just the outside coming back with: `some_name` ... – TheWebs Nov 29 '13 at 03:54
  • can you do a `console.log(typeof fn)` outside the `if` – Arun P Johny Nov 29 '13 at 03:55
  • it comes back and sais string. I am passing by reference, as stated in your example, not by string name. – TheWebs Nov 29 '13 at 03:57
  • @KyleAdams no... you are passing it as a string... otherwise it should have returned function – Arun P Johny Nov 29 '13 at 03:58
  • I legit coppied your example, replaced url and data with my stuff and ran it and got string instead of function – TheWebs Nov 29 '13 at 03:59