Are functions underestimated in JavaScript named?

Taking the jQuery framework, for example, if you run code like this:

$(document).ready(function init() { foo.bar(); }); 

The stack trace in Firebug will look like this:

 init() anonymous() anonymous([function(), init(), function(), 4 more...], function(), Object name=args) anonymous() anonymous() 

As you can see, this is not very readable because you need to click on each function to find out what it is. Anonymous functions are also displayed as (?)() In the profiler, and they can lead to the error " cannot access optimized close ". It seems to me that these are good reasons to avoid them. Then there is the fact that ECMAScript 5 will condemn arguments.callee in its strict mode, which means that it will not be possible to refer to anonymous functions with it, which makes them a little less reliable for the future.

On the other hand, the use of these functions can lead to repetition, for example:

 var Foo = { bar: function bar() {} } function Foo() {} Foo.prototype.bar = function bar() {} 

Do I correctly believe that this repetition is justified in the light of the convenience of debugging called function functions, and that the prevalence of anonymous functions in a good framework such as jQuery is an oversight?

+6
javascript function jquery debugging expression
source share
4 answers

I found the answer to my question in this very informative article . First, it turns out that I was right when named functions are more desirable, but the solution is not as simple as adding identifiers to all anonymous functions. The main reason for this is JScript, which implements functional expressions very strongly.

Secondly, there is a difference between function operators and expressions. An anonymous function is just an expression of a function with a missing identifier, and adding an identifier (naming) will not make it an instruction (with the exception of JScript, so it is broken). This means that all other answers do not match.

+3
source share

I agree that there are some drawbacks to using anonymous methods in JavaScript / EMCAScript. However, do not forget how to use them. For simple liners that you want to transfer to another function, they are often excellent.

+3
source share

But for me, anonymous functions are more readable in the source code, because I am sure that they are used only there.

+1
source share

Anonymous functions are very convenient. The best solution to this problem, instead of calling functions, would be if firebug told you on which line in which file the anonymous function was created.

 init() anonymous() // application.js, line 54 anonymous() // foo.js, line 2 

And the stack trace is the only place where anonymous functions are an imo problem.

0
source share

All Articles