I use anonymous functions for three reasons:

  1. If no name is needed because the function is only ever called in one place, then why add a name to whatever namespace you're in.
  2. Anonymous functions are declared inline and inline functions have advantages in that they can access variables in the parent scopes. Yes, you can put a name on an anonymous function, but that's usually pointless if it's declared inline. So inline has a significant advantage and if you're doing inline, there's little reason to put a name on it.
  3. The code seems more self-contained and readable when handlers are defined right inside the code that's calling them. You can read the code in almost sequential fashion rather than having to go find the function with that name.

I do try to avoid deep nesting of anonymous functions because that can be hairy to understand and read. Usually when that happens, there's a better way to structure the code (sometimes with a loop, sometimes with a data table, etc...) and named functions isn't usually the solution there either.

I guess I'd add that if a callback starts to get more than about 15-20 lines long and it doesn't need direct access to variables in the parent scope, I would be tempted to give it a name and break it out into it's own named function declared elsewhere. There is definitely a readability point here where a non-trivial function that gets long is just more maintainable if it's put in its own named unit. But, most callbacks I end up with are not that long and I find it more readable to keep them inline.

What are the benefits to using anonymous functions instead of named functions for callbacks and parameters in JavaScript event code?的更多相关文章

  1. Oracle Single-Row Functions(单行函数)——NULL-Related Functions

    参考资料:http://docs.oracle.com/database/122/SQLRF/Functions.htm#SQLRF006 Single-row functions return a ...

  2. SQL Fundamentals || Single-Row Functions || 日期函数date functions

    SQL Fundamentals || Oracle SQL语言   SQL Fundamentals: Using Single-Row Functions to Customize Output使 ...

  3. SQL Fundamentals || Single-Row Functions || 数字函数number functions

    SQL Fundamentals || Oracle SQL语言 SQL Fundamentals: Using Single-Row Functions to Customize Output使用单 ...

  4. SQL Fundamentals || Single-Row Functions || 字符函数 character functions

    SQL Fundamentals || Oracle SQL语言   SQL Fundamentals: Using Single-Row Functions to Customize Output使 ...

  5. Clausen Functions (and related series, functions, integrals)

    Since the Clausen functions are intimately related to a number of other important special functions, ...

  6. 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)

    NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.

  7. Node.js 开发模式(设计模式)

    Asynchronous code & Synchronous code As we have seen in an earlier post (here), how node does th ...

  8. Understand JavaScript Callback Functions and Use Them

    In JavaScript, functions are first-class objects; that is, functions are of the type Object and they ...

  9. 【JavaScript】Understanding callback functions in Javascript

    Callback functions are extremely important in Javascript. They’re pretty much everywhere. Originally ...

随机推荐

  1. linux sort命令用法

    sort命令:用于将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序. 命令格式: sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符> ...

  2. 七年开发小结MyBatis 在 Spring 环境下的事务管理

    MyBatis的设计思想很简单,可以看做是对JDBC的一次封装,并提供强大的动态SQL映射功能.但是由于它本身也有一些缓存.事务管理等功能,所以实际使用中还是会碰到一些问题——另外,最近接触了JFin ...

  3. 前端框架开始学习Vue(一)

    MVVM开发思想图(图片可能会被缩小,请右键另存查看,图片来源于网络)   定义基本Vue代码结构   1 v-text,v-cloak,v-html命令 默认 v-text没有闪烁问题,但是会覆盖元 ...

  4. CentOS7安装CDH 第十章:CDH中安装Spark2

    相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...

  5. c# 串行化事件

  6. Django之过滤器

    Django 过滤器   过滤器 描述 示例 upper 以大写方式输出 {{ user.name | upper }} add 给value加上一个数值 {{ user.age | add:”5” ...

  7. CentOS7.x安装nginx

    1.安装先决条件 yum install yum-utils 2.设置yum存储库和创建/etc/yum.repos.d/nginx.repo 使用以下内容命名的文件 :稳定版 [nginx-stab ...

  8. 删除svn用户

    以win7为例 1.进入c:/Users/[你的用户名]/AppData/Roaming/Subversion/auth目录,删除该目录下的所有文件: 2.重启eclipse/myeclipse,提交 ...

  9. 记一个VS连接过程中找不到cpp的解决方法

    在新增几个qt页面时,发现原来没动的几个cpp 连接报错了,错误均是qt的相关文件找不到 应该是moc文件没有生产或者没有被包含进工程.我想着既然我没动,应该不会是moc的原因,就在其他方向解决了很久 ...

  10. Mac OS下安装MongoDB以及配置方法总结【笔记】

    首先打开命令框,输入: brew install mongodb 安装完成后  启动.停止.重启如下 brew services start mongodb brew services stop mo ...