What are the advantages and disadvantages of anonymous functions? -

Source: Internet
Author: User
Tags joyent
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.