I'm using firebug here, and trying to write a blog post to demonstrate something just like these code.
// Unnamed Anonymous Function
var count1 = function () {
var x = 0, f;
f = function () {
x = x + 1;
return x;
};
return f;
};
// Named Anonymous Function
var count2 = function cf() {
var x = 0, f;
f = function ff() {
x = x + 1;
return x;
};
return f;
};
var c = count1();
console.log(count1); // function()
console.log(c); // function()
var d = count2();
console.log(count2); // cf()
console.log(d); // ff()
The point is, firebug logs Anonymous Functions as just function()
, and even you click the link it navigates you to the script tab but sometimes in wrong position, your Anonymous Function isn't there.
But if you name it like cf()
or ff()
you can easily recall what function it is.
In this example the code is short, but if you work in large scale application, e.g. in ASP.net Web Form script resource, naming for function may somehow save your day when debug ?
P.S. I prefer Function Expression over Function Declaration.