JavaScript often sees the use of anonymous functions, but some people are opposed to the use of anonymous functions. Is it good for us to use anonymous functions? JavaScript often sees the use of anonymous functions, but some people are opposed to the use of anonymous functions. Is it good for us to use anonymous functions? Reply content: who opposed it? The disadvantage of anonymous functions is that they are not convenient for subsequent retrieval and identification (Most anonymous functions do not have such requirements in reasonable use scenarios). However, this has been improved in ES6, each function in ES6 has a name attribute for easy identification. In some cases, it is unreasonable to use an anonymous function. It is wrong to use a stick to completely kill the function.
Subject: choose whether to use anonymous functions based on the scenario.
For example, in the callback:
You can choose to use it because its name is not so important:
Http. createServer (function (req, res) {// do something ...});
The disadvantage is .. Anonymous function events .. Cannot unbind... It is really a headache to get the name. When registering a function event, do not use an anonymous function ,,,,,
Otherwise, you cannot unbind it.
Of course, if you use a framework, the framework will help you solve the problem. Debugging is one of the scenarios where anonymous functions are not suitable for use,
For example, in nodejs, if you use the debugging plug-in written by corner stone to track the usage of a statement error, the system will directly tell you which function has a problem. If the function has a name, directly display the name, but there is no name, you can directly write anonymous
Another scenario is that the callback is only used once. Generally, it is packaged with onlyOnce. If the second call is made, it will show which function has an error. This is the improvement of async.
In the nodejs package provided by windows Asure, you will find that the prototype method name is added to all functions corresponding to the prototype method.
Joyent recommends that you use short and lean functions with names.
Joyent does not encourage the use of closures. If the program grows and the number of layers of stack cross-origin calls increases, maintenance will be very difficult. One of the notable features of closures is that anonymous Callbacks are reasonable, as said above, who has opposed it?