2

I just wanted to understand the general advantages and usages of declared functions versus anonymous functions:

Declared function:

function func (array, callback) {
};

versus

Anonymous function:

function func (array, function(element) {

});

I'm specifically comparing the advantages/usages of callback and function(element) {} in practice.

Thank you!

My Name
  • 137
  • 1
  • 13
  • 6
    The benefit of declared functions is that you can reference them in multiple places. The benefit of anonymous functions is that you don't have to declare them. That should more or less give you all you need to know to decide which to use. – Ant P Feb 22 '15 at 18:06
  • 2
    not sure this is a good fit question for SO, but I'll throw in one benefit: in debugger output (e.g. stack traces from errors), declared functions will show up by name. – Stephen Thomas Feb 22 '15 at 18:06
  • 1
    ... thats not even a valid syntax.. you meant var func=function(){}; ? btw, anon functions can be useful to not clog up the namespace :p addEventListener("foo",function callbackfunc(){}) – hanshenrik Feb 22 '15 at 18:10
  • Also, a declared function can be called before its declaration appears in the code, because declarations are parsed before runtime. Whereas assigning a function to a variable declared with `var` doesn't allow calling that variable before the assignment. – Touffy Feb 22 '15 at 18:25
  • You might want to read http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html and [var functionName = function() {} vs function functionName() {}](http://stackoverflow.com/q/336859/1048572). Please [edit] your question to clarify what you want to know. – Bergi Feb 22 '15 at 23:43

0 Answers0