开发者

Are anonymous functions a bad practice in JavaScript?

I was reading that using anonymous functions in javascript is bad practice, because it can make debugging a pain, but I haven't seen this for myself. Are anonymous functions in JavaScript really bad practice a开发者_如何学运维nd, if so, why?


I am going to go against the flow a little here and make the case that anonymous functions are indeed bad practice even though they are widely used.

1) Anonymous functions cannot be reused.

2) Anonymous functions, by definition, do not have a name and so do not describe what they do. Which is to say the code is not self documenting.

3) Anonymous functions cannot be tested in isolation with a unit testing framework.

4) I personally think they make code more difficult to read and debug. Though your experience may vary.

I do think there are situations where an anonymous function is the best choice and as a general rule in order to avoid the above downsides I almost always name my functions.

Typically the longer your anonymous function becomes the more likely that it would benefit from having a name.


Nope, anonymous functions are used all over the place in JavaScript across the web. It may make debugging a little more difficult in spots, but not nearly enough to say that they shouldn't be used.

For example, JQuery makes extensive use of them.

There are a lot of times when you want to use them over formally declared functions, such as when you want to limit their scope.


I would say the contrary, lambdas ( alias ) make some expressions much more succinct. If you're binding multiple event handlers to multiple events it would be tedious giving a function name to each and every event handler, for example.

It's more helpful and time-conserving than not, even if it makes debugging a little bit harder but I rarely struggle with debugging because a function is anonymous. And you should use JSLint to make your life easier when coding.


Just because everybody uses them doesn't make them good practice (everybody remember using the table element for layout?). But, they're great because they can help clarify and simplify your code, giving less opportunity for something to go wrong.

But, anonymous functions shouldn't be so complicated that debugging becomes difficult with them. In that case, perhaps it's better to make a new function.


Most definitely not, lambda functions are used all over the place, almost ubiquitous.


Here is my browser console:

// Bad
poopy = function(){}
// function (){}
groupy = poopy;
// function (){}

// Good
droopy = function loopy(){};
// function loopy(){}
floupy = droopy;
// function loopy(){}   

Imagine you are debugging something and you have a function name called groupy. You type it's name to get more information on it. If that function has been set as in the bad section, you have no idea of what the original declaration was. If however, you define your function name, as in the Good section, then you will always have a trace of the original function name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜