With the trend of using function expressions to declare functions, does this mean that the state of JavaScript has moved to be where we are programming using almost exclusively anonymous functions?
We are used to the idea of anonymous functions as being like the ones passed to the forEach method of arrays:
array.forEach(function () {
// an anonymous function
});
However, when we assign a function to a variable, we are also creating an anonymous function:
var someFunc = function () {
// this is an anonymous function too
};
And of course, with more structured techniques we frequently use anonymous functions too:
var project = (function () {
// an anonymous function, normally acceptable
...
return {
init: function () {
// yet more anonymous functions
...
},
...
};
}());
We can deal with that by naming the functions, but then we end up duplicating the function’s name:
var someFunc = function someFunc() {
// now a named function
};
The above tends to look somewhat excessive due to the duplication of the function name, which prevents some from using it as a common programming technique.
We can always use function declarations, but they are being denigrated these days because people don’t like to deal with hoisting issues:
function someFunc() {
// a function declaration
}
Are there problems with this trend of using anonymous functions? It can certainly make debugging more difficult because you no longer have access to the name of the functions that are involved.
Are there any other issues with anonymous functions? Or is it just something that we have to learn to live with now that we are programming with function expressions?