资讯专栏INFORMATION COLUMN

Javascript anonymous functions

刘福 / 1313人阅读

Javascript anonymous functions
  

Anonymous functions are functions that are dynamically declared at runtime. They’re called anonymous functions because they aren’t given a name in the same way as normal functions.

  

Anonymous functions are declared using the function operator instead of the function declaration.

命名函数:

function flyToTheMoon()
{
  alert("Zoom! Zoom! Zoom!");
}
flyToTheMoon();

匿名函数:

var flyToTheMoon = function()
{
  alert("Zoom! Zoom! Zoom!");
}
flyToTheMoon();
Anonymous functions are created using the function operator

在JavaScript中创建函数最常用的有两种方法:函数声明和函数赋值表达式。匿名函数的创建使用的是函数赋值表达式。直接贴两张图,简单明了:

函数声明:

函数赋值表达式:

在调用函数赋值表达式(function operator)会创建一个新的函数对象,然后把返回它。这里就是把创建的函数对象赋值给flyToTheMoon。

这个赋值,跟把普通函数返回的值赋给变量是一样的,比较特殊的就是匿名函数的值是一个函数对象而不是普通的如字符串或者时间这些变量。这可能是因为在JavaScript中函数就是一种特殊类型的对象,意味着你可以按操作其他对象一样来操作它。它们能存储在变量中,能作为参数传递给其它函数,也可以通过return语句从其它函数里返回。函数总是对象,无论你是怎么创建的。

Anonymous functions are created at runtime
  

The function operator can be used anywhere that it’s valid to use an
expression. For example you can use the function operator when a
variable is being assigned, when a parameter is being passed to a
function or in a return statement. This is possible because the
function operator is always invoked at runtime.

Function declarations are different. They are run before any of the
other code is executed so the functions do not have to be declared
before the code that calls them.

Function declarations can’t be used to create anonymous functions
because they require the function to have a name. The function
declaration uses the function name to add it as a variable in the
current scope.

Anonymous functions don’t have a name

匿名函数没有函数名,那怎么能调用呢?可以调用是因为函数名存的是一个函数对象,而不像普通变量。

函数声明创建函数时,总是有一个函数名,还有一个有编译器自动生成的和函数名的同名变量。

对于函数赋值表达式创建函数时,函数名是可选的。很多情况下,创建一个匿名函数时,函数名对我们来说是不重要的。就像这样:

var flyToTheMoon = function() {
   alert("Zoom! Zoom! Zoom");
}
flyToTheMoon();

不过使用函数赋值表达式也可以设置函数名,这个函数和上面这个函数一模一样,只是多了个函数名而已:

var flyToTheMoon = function flyToTheMoon() {
   alert("Zoom! Zoom! Zoom");
}
flyToTheMoon();
  

Giving your function a name does not automatically add a variable into scope with the function name. You still need to assign the return value of the function operator a variable.

上面这个例子中,函数名和存储函数对象的变量名是一样的,其实变量名也可以和函数名不一样,如:

var thingsToDoToday = function flyToTheMoon() {
   alert("Zoom! Zoom! Zoom");
}
thingsToDoToday();
Why have a name?

添加一个函数名,看起来多余,其实还是很有用的,比如在递归函数中,可以通过函数名调用自身,如下:

var thingsToDoToday = function flyToTheMoon() {
  if(!onTheMoon)
    flyToTheMoon();
  else
    alert("One small step for a man..");
}
thingsToDoToday();
  

It can also useful for debugging because you can see the function’s
name in a call stack. Anonymous functions generally all look the same in the call stack. If you have a nasty debugging situation, sometimes giving names to the functions you are interested in can make things clearer

Why are anonymous functions useful?

Not having to set a name for an anonymous function is just a convenience thing since in most cases the name of the function doesn’t really matter. Most of the time anonymous functions and named functions will both do any job perfectly well.

Functions created with the function operator can be very useful. See some examples of where it can be used.

本文截取自:http://helephant.com/2008/08/23/javascript-anonymous-functions/

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/85405.html

相关文章

  • 分享一个关于匿名函数和闭包的问答

    摘要:引用一个的提问个人觉得总结的比较好的两句话原文地址另外,附上中对闭包的讲解闭包中文对于闭包的简要概括原文原文地址匿名函数和闭包来自文章作者版权声明自由转载非商用非衍生保持署名创意共享许可证转载请注明出处 引用一个stackoverflow的提问 个人觉得总结的比较好的两句话: An anonymous function is just a function that has no na...

    Barrior 评论0 收藏0
  • JavaScript阴沟里翻船之运算符优先级

    摘要:操作符的两种形态其实在的操作符描述中,语法是你会发现被中括号所包围也就意味着可缺省,因此,如果对于不含参数的构造函数而言与二者并无区别,那我们接着思考一个问题,对于前面返回函数的而言,当的时候为什么执行的是而不是呢。  首先欢迎大家关注我的Github博客,也算是对我的一点鼓励,毕竟写东西没法变现,坚持下去也是靠的是自己的热情和大家的鼓励。各位读者的Star是激励我前进的动力,请不要吝...

    selfimpr 评论0 收藏0
  • 浅谈Javascript闭包和匿名函数【1】

    摘要:我们可以用普通函数内部嵌套匿名函数,形成一个闭包来使变量驻留在内存中。局部变量闭包为什么要将赋值给变量呢这里我们就要谈到匿名函数调用问题匿名函数如何调用还是上面的例子会将整个函数体打印出来这样才调用了函数内部的匿名函数看到这里。 闭包含义: 闭包是指有权访问另一个函数作用域中的变量的函数,创建闭包的常见的方式,就是在一个函数内部创建另一个函数,通过另一个函数访问这个函数的局部变量。 这...

    cyqian 评论0 收藏0
  • Elm入门实践(二)——类型篇

    摘要:如果不声明类型呢如果注释掉类型注解重新编译,还是会报错,只是错误信息变了,这次是第行即使没有显式的类型注解,的类型推导系统也会发挥作用,此处通过类型推导认为函数的参数应该是字符串,但是传入了数字,因此报错。 记得Facebook曾经在一次社区活动上说过,随着他们越来越多地使用Javascript,很快就面临了曾经在PHP上遇到的问题:这东西到底是啥? 动态语言就像把双刃剑,你可以爱死它...

    ZHAO_ 评论0 收藏0
  • ES6 Symbol ,对象匿名(anonymous)属性实现

    摘要:在运行时环境中,通过调用函数创建值,该函数动态生成匿名的唯一值。创建和使用值的唯一创建方法,是通过调用函数来返回,不支持操作。共享体系提供了一个全局注册表,用于在大文件或多文件代码中追踪值。 Symbol由来 Symbol是ES6引入的新类型,所以在ES5的基础上,JS就有了字符串(string)、数字型(number)、布尔(bool)、null、undefined和Symbol共六...

    JowayYoung 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<