[英]Why would you assign a function to a variable instead of declaring a named function?
Why would I do this: 我为什么要这样做:
var myfunc = function() { /* code */ };
...
myfunc();
instead of this: 代替这个:
function myfunc() { /* code */ }
...
myfunc();
Are there any benefits of using one over the other? 相对于其他使用,有什么好处吗? I have seen both examples used in different places. 我已经看到了在不同地方使用的两个示例。
The only difference as far as I can tell is that the anonymous function cannot call itself recursively while the named function can. 据我所知,唯一的区别是匿名函数不能递归调用自身,而命名函数可以。 There is a third type of construct that combines both of these, ie you can have a named function expression: 结合了这三种的构造是第三种类型,即可以有一个命名函数表达式:
var myfunc = function myfunc() { /* code */ };
If a function is declarated normally, the function name (its identifier) will not be deleteable even if the identifier is redeclared. 如果正常声明了一个函数,则即使重新声明了该标识符,该函数名(其标识符)也将不可删除。 The identifier will only be deleted when its scope ends. 该标识符仅在其作用域结束时才会被删除。
function myfunc() { /* code */ };
if (delete myfunc) { //will fail
alert('myfunc deleted');
} else {
alert('can not delete myfunc');
}
myfunc = null;
if (delete myfunc) { //will still fail
alert('myfunc deleted');
} else {
alert('can not delete myfunc');
}
var myfunc = null;
if (delete myfunc) { //will still fail
alert('myfunc deleted');
} else {
alert('can not delete myfunc');
}
But if a function declaration is assigned to a variable, its identifier can be deleted. 但是,如果将函数声明分配给变量,则可以删除其标识符。 This is especially useful when you need to create a global function but only use it temporarily, so that it can be deleted when it's no longer needed or to avoid possible identifier conflit with third party scripts. 当您需要创建全局函数而仅临时使用它时,此功能特别有用,以便可以在不再需要它时将其删除,或避免可能的标识符与第三方脚本冲突。
var myfunc = function() { /* code */ };
if (delete myfunc) { //will succeed
alert('myfunc deleted');
} else {
alert('can not delete myfunc');
}
//or...
var myfunc = function myrealfunc() { /* code */ };
if (delete myfunc) { //will succeed
alert('myfunc deleted');
} else {
alert('can not delete myfunc');
}
There are a few differences, mostly pragmatic. 有一些差异,大多数是实用的。 When you 'var' a function, the normal assumption is some kind of 'locally' scoped function ( think a nested function ). 当您“变”一个函数时,通常的假设是某种“局部”范围的函数(以嵌套函数为例)。 When you do, function myFunction() {}
, the function is mostly assumed to be globally scoped (though you could wrap this inside an anonymous function as well). 当您执行function myFunction() {}
,通常假定该函数是全局范围的(尽管您也可以将其包装在匿名函数中)。
In the case of a javascript 'class', if you want to create a locally scoped function, you would have to use 'var' to declare it. 对于javascript“类”,如果要创建本地范围内的函数,则必须使用“ var”进行声明。
var myClass = function() {
var test = function() {
//This function has local scope
}
};
Adding to the above comments, this is a simple example. 除了上述注释之外,这是一个简单的示例。
var myVariable = resultFunction();
function resultFunction() {
return 1;
}
The above code will work. 上面的代码将起作用。 But you can't do the following 但是您不能执行以下操作
var myVariable = resultFunction();
var resultFunction = function() {
return 1;
};
But you can do 但是你可以做
var resultFunction = function() {
return 1;
};
var myVariable = resultFunction();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.