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/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。