As the title says, whats the difference between
正如标题所说,最重要的是什么
MyFunction = function() {
}
and
function MyFunction() {
}
Nothing?
Duplicate: var functionName = function() {} vs function functionName() {}
重复:var functionName = function(){} vs function functionName(){}
5 个解决方案
#1
1
The first form is actually a variable with an anonymous function assigned to it, the second is a declared function.
第一种形式实际上是一个赋予它的匿名函数的变量,第二种形式是声明的函数。
They're almost interchangeable, but have some differences: debuggers have a harder time with anons (because they lack a name) and the JS engine can directly access declared functions wherever they exist in the script, but anons can't be accessed until they have been assigned.
它们几乎可以互换,但有一些区别:调试器有更难的时间(因为它们没有名称),JS引擎可以直接访问声明的函数,无论它们存在于脚本中的哪个位置,但是在它们之前无法访问它们已被分配。
#2
0
I think you mean
我想你的意思是
var MyFunction = function() {
}
And in many cases, there is little difference.
在许多情况下,几乎没有什么区别。
The var syntax is sometimes useful when you are inside some sort of wrapping class and want to be sure of your namespace (like in greasemonkey).
当你在某种包装类中并想要确定你的命名空间时(例如在greasemonkey中),var语法有时很有用。
Also, I believe the function-are-constructors stuff (with .prototype, etc) do not work with the var syntax.
另外,我相信函数构造函数(使用.prototype等)不能与var语法一起使用。
#3
0
here is one difference:
这是一个区别:
function Moose() {
alert(arguments.callee.name); // "Moose"
}
var Cow = function() {
alert(arguments.callee.name); // ""
}
#4
0
function myFunction(){}
is the same as var myFunction = function myFunction(){}
. If you just do MyFunction = function(){}
, it's like MyFunction = function anonymous(){}
.
function myFunction(){}与var myFunction = function myFunction(){}相同。如果你只是做MyFunction = function(){},它就像MyFunction = function anonymous(){}。
The variables and functions have different scopes but the function can only be referenced outside the function via the variable name, which needs to be bound to the function.
变量和函数具有不同的范围,但该函数只能通过变量名称在函数外部引用,该变量名称需要绑定到函数。
function myFunction(){}
binds the myFunction
variable to a function that just happens to have the same name.
function myFunction(){}将myFunction变量绑定到恰好具有相同名称的函数。
#5
-1
There's one subtle difference on Firefox, when you're declaring a window load
event handler. Declaring the following in a script tag only works on Firefox:
当你声明一个窗口加载事件处理程序时,Firefox上有一个细微的区别。在脚本标记中声明以下内容仅适用于Firefox:
function onload() {
alert(42);
}
You have to do it like this for IE, Opera, Safari or Chrome:
对于IE,Opera,Safari或Chrome,你必须这样做:
onload = function () {
alert(42);
}
#1
1
The first form is actually a variable with an anonymous function assigned to it, the second is a declared function.
第一种形式实际上是一个赋予它的匿名函数的变量,第二种形式是声明的函数。
They're almost interchangeable, but have some differences: debuggers have a harder time with anons (because they lack a name) and the JS engine can directly access declared functions wherever they exist in the script, but anons can't be accessed until they have been assigned.
它们几乎可以互换,但有一些区别:调试器有更难的时间(因为它们没有名称),JS引擎可以直接访问声明的函数,无论它们存在于脚本中的哪个位置,但是在它们之前无法访问它们已被分配。
#2
0
I think you mean
我想你的意思是
var MyFunction = function() {
}
And in many cases, there is little difference.
在许多情况下,几乎没有什么区别。
The var syntax is sometimes useful when you are inside some sort of wrapping class and want to be sure of your namespace (like in greasemonkey).
当你在某种包装类中并想要确定你的命名空间时(例如在greasemonkey中),var语法有时很有用。
Also, I believe the function-are-constructors stuff (with .prototype, etc) do not work with the var syntax.
另外,我相信函数构造函数(使用.prototype等)不能与var语法一起使用。
#3
0
here is one difference:
这是一个区别:
function Moose() {
alert(arguments.callee.name); // "Moose"
}
var Cow = function() {
alert(arguments.callee.name); // ""
}
#4
0
function myFunction(){}
is the same as var myFunction = function myFunction(){}
. If you just do MyFunction = function(){}
, it's like MyFunction = function anonymous(){}
.
function myFunction(){}与var myFunction = function myFunction(){}相同。如果你只是做MyFunction = function(){},它就像MyFunction = function anonymous(){}。
The variables and functions have different scopes but the function can only be referenced outside the function via the variable name, which needs to be bound to the function.
变量和函数具有不同的范围,但该函数只能通过变量名称在函数外部引用,该变量名称需要绑定到函数。
function myFunction(){}
binds the myFunction
variable to a function that just happens to have the same name.
function myFunction(){}将myFunction变量绑定到恰好具有相同名称的函数。
#5
-1
There's one subtle difference on Firefox, when you're declaring a window load
event handler. Declaring the following in a script tag only works on Firefox:
当你声明一个窗口加载事件处理程序时,Firefox上有一个细微的区别。在脚本标记中声明以下内容仅适用于Firefox:
function onload() {
alert(42);
}
You have to do it like this for IE, Opera, Safari or Chrome:
对于IE,Opera,Safari或Chrome,你必须这样做:
onload = function () {
alert(42);
}