简体   繁体   English

为什么要为变量分配一个函数而不是声明一个命名函数?

[英]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.

相关问题 为什么要为命名变量分配函数声明? - Why would I assign a function declaration to a named variable? 为什么要将全局变量传递给函数? - Why would you pass a global variable to a function? 为什么在 object 的 function 属性中声明变量之前可以引用它? - Why can you reference a variable before declaring it in a function property of an object? 为什么不将变量声明为函数的参数? - Why not declaring a variable as an argument of a function? 为什么要将此分配给另一个变量? - why would you assign this to another variable? 在javascript中声明存储在函数数组中的函数时,为变量赋值 - Assign value to a variable when declaring a function stored in a function array in javascript 为什么将名为value的变量声明为对象会返回Firefox中的函数类型 - Why does declaring a variable named values as an object returns type of function in Firefox 在 Javascript 中,什么时候需要将命名函数分配给变量? - In Javascript, when is it necessary to assign a named function to a variable? 声明后执行命名函数 - Executing a named function after declaring it 为什么调试器会给您“未定义的不是函数”,而不仅仅是告诉您不是函数的属性/变量的名称? - Why does debuggers give you “undefined is not a function” instead of just telling you the name of the property/variable that is not a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM