标签云

微信群

扫码加入我们

WeChat QR Code

这意味着当一个变量是一个函数是什么?[重复]

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}

In JavaScript, what's the purpose of defining a variable as a function? I've seen this convention before and don't fully understand it.

For example, at some point in a script, a function is called like this:

whatever();

But where I would expect to see a function named whatever, like this:

function whatever(){

}

Instead I'll see a variable called whatever that's defined as a function, like this:

var whatever = function(){

}

What's the purpose of this? Why would you do this instead of just naming the function?


They're called anonymous functions: helephant.com/2008/08/23/javascript-anonymous-functions

2018年06月20日19分40秒

great and usefull, thanks : )

2018年06月19日19分40秒

DanielAndresAcevedo: Thanks. It was also slightly out of date, I've added a note at the end.

2018年06月19日19分40秒