Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using named functions for callbacks

Tags:

javascript

I have a major problem with profiling in javascript with anonymous functions, I have always many anonymous functions - most of them are callbacks - and It makes analyzing results of profiler very hard for me.

Finally I decided to use named functions for callbacks, like this:

var f = function(callback) {
    // Do something ...
    callback();
}

f(function named_function() {
    console.log('Sample callback function!');
});

I want to know that will I have any problems after making this change in my codes? And will this type of function definition and passing reserve the name (named_function) anywhere?

like image 331
MostafaR Avatar asked Aug 27 '12 17:08

MostafaR


People also ask

Can a callback function call another function?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.

Can you use an anonymous function in a callback?

In JavaScript, callbacks and anonymous functions can be used interchangeably.

How do you assign a callback function?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function.

Are callback functions good?

The callback function — including its references — is passed as an argument to other functions. The functions that receive the callback function as a parameter are the ones responsible to call back the callback function. Callbacks are great because they open up a lot of programming possibilities.


2 Answers

The name will only be available inside the scope of the named function expression.

But there is a problem in IE 8 and lower. It will leak out to the outer scope, and will actually create a different function object, so you should nullify it if that's a problem.

f(function named_function() {
    console.log('Sample callback function!');
});
var named_function = null;

See this article for more information: Named function expressions demystified

Or you could create it like this to solve the IE issue.

f(function() {
    return function named_function() {
        console.log('Sample callback function!');
    };
}());

But that's a little ugly.

like image 108
gray state is coming Avatar answered Oct 16 '22 07:10

gray state is coming


If you pass anonymous functions like that, the name will exist inside the function itself.

It will not exist in any other scope.

var f = function(callback) {
    // Do something ...
    callback();
}

f(function named_function() {
    console.log(named_function); // Logs the function
    console.log('Sample callback function!');
});

console.log(named_function);​ // Error: named_function is undefined
like image 25
Rocket Hazmat Avatar answered Oct 16 '22 07:10

Rocket Hazmat