user656925
user656925

Reputation:

When to use anonymous functions

I have an object ajax_tryit which calls ajax_generic ans sends it 3 functions. They are all named.

Would it be better (more efficient, a tad faster) to use anonymous functions.

Application...This is the ajax call back function which can do 3 things, pass, fail, or undefined(usually a php error).

function ajax_generic( server_response_text, pass_func, fail_func, undefined_func )
{
    var aml_status = check_aml( server_response_text.slice( 0, 6 ) );
    if( aml_status === Constant.AML.PASS )
    {
        pass_func();
    }
    else if( aml_status === Constant.AML.FAIL )
    {
        fail_func();
    }
    else
    {
        undefined_func();
    }
}
function ajax_tryit( server_response_text, html_div )
{
    var pass_func = function {window.location.reload()};
    var fail_func = function(server_response_text) { alert( 'ajax_tryit(): ' + server_response_text ) } ;
    var undefined_func = function(server_response_text) { alert( 'php error: ' + server_response_text ) };
    ajax_generic( pass_func, fail_func, undefined_func );
}

Upvotes: 1

Views: 505

Answers (3)

Patrick Evans
Patrick Evans

Reputation: 42746

Anon functions are good to use when you need to pass variables to callback functions or change the scope

Example (passing arguments to callback function):

var somevar = "test";

setInterval( function()
{
    test(somevar);
},5000);

function test(Msg)
{
    alert(Msg);
}

if you had just done

setInterval(test,5000);

you would have just gotten a blank alert box, extremely simplified example.

For changing scope look at the answer given there it shows an example of when needing to change scope.

But other then that there is no real need to switch out named functions for anon ones.

Upvotes: 1

Ry-
Ry-

Reputation: 225281

It always depends on which is more clear, and so in this case I'd say the answer is no. Right now you have fairly descriptive names. Taking them out would make code harder to maintain, and would make indentation confusing. They don't pose any problems by having names.

One thing you could change, though, is the indentation and syntax:

function ajax_tryit(server_response_text, html_div) {
    function pass_func() {
        window.location.reload()
    }

    function fail_func(server_response_text) {
        alert('ajax_tryit(): ' + server_response_text)
    }

    function undefined_func(server_response_text) {
        alert('php error: ' + server_response_text)
    }

    ajax_generic(pass_func, fail_func, undefined_func);
}

EDIT: Also, in reply to your comment:

Save 3 variables..so a tad more efficient.

No, it's not more efficient not to use variables, especially here. Don't worry about that.

Upvotes: 4

Pointy
Pointy

Reputation: 414086

Named functions (that is, functions created with function declaration statements) are actually pretty nice because you can see their names in stack traces. Other than that, it doesn't much matter so long as the functions are declared in the appropriate scope (or if the scope doesn't matter).

Giving a function a name in a function definition expression is technically legal, but it's not a good idea because the various JavaScript engines can't be trusted not to do something weird.

Upvotes: 6

Related Questions