简体   繁体   English

JavaScript中的命名和未命名函数

[英]Named and unnamed functions in JavaScript

What is the nature of the data structure used to store the name of a function in JavaScript? 用于在JavaScript中存储函数名称的数据结构的本质是什么?

In other words, where is the string "Foo" (ie the name of the function Foo ) stored when this code is evaluated? 换句话说,在评估此代码时,字符串“ Foo”(即函数Foo的名称)存储在哪里?

function Foo() {}

Also, do anonymous functions have a hidden name? 另外,匿名函数是否具有隐藏名称?

where is the string "Foo" (ie the name of the function Foo) stored 字符串“ Foo”(即函数Foo的名称)存储在哪里

In the global scope. 在全球范围内。

You can avoid this by using a self invoking anonymous function : 您可以通过使用自调用匿名函数来避免这种情况:

(function() {
    alert('Hello World');
})();

, or by associating a var to a function within a local scope : ,或通过将var与本地范围内的函数相关联:

function myBigFunction() {
var myfunction = function foo(){alert('Hello World');};
}

No hidden name. 没有隐藏的名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM