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 );
}
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.