javascript 函数声明的范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14665160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:16:35  来源:igfitidea点击:

javascript scope of function declarations

javascriptfunctionscope

提问by user1816847

The varkeyword in javascript causes a variable to be stored in the local scope. Without varvariables belong to the global scope. What about functions? It's clear what happens when functions are declared like variables

varjavascript 中的关键字导致将变量存储在本地范围内。没有var变量属于全局范围。函数呢?很明显当函数像变量一样声明时会发生什么

var foo = function() {...}

but what scope does

但范围是什么

function foo() {...} 

belong to?

属于?

EDIT: I realized I didn't ask quite the right question so as a follow up. In the outer most nesting is there a difference between the above two declarations and the following declaration?

编辑:我意识到我没有问正确的问题所以作为跟进。在最外层的嵌套中,上面两个声明和下面的声明有区别吗?

foo = function() {...}

回答by bfavaretto

It belongs to the currentscope, always. For example:

它始终属于当前范围。例如:

// global scope

// foo is a global function
function foo() {

    // bar is local to foo
    function bar() {

    }

}

Regarding your second question, this:

关于你的第二个问题,这个:

foo = function() {...}

is an anonymous function expression assigned to a global variable (unless you're running is strict mode, then foowould be undefined). The difference between that and function foo() {}is that the latter is a function declaration(versus a variabledeclaration, which is assigned an anonymous function expression).

是分配给全局变量的匿名函数表达式(除非您在严格模式下运行,否则foo将是未定义的)。两者之间的区别在于function foo() {}后者是一个函数声明(相对于变量声明,它被分配了一个匿名函数表达式)。

You might be interested in this excellent article about function declarations and function expressions: Named function expressions demystified.

您可能对这篇关于函数声明和函数表达式的优秀文章感兴趣:命名函数表达式解密

回答by Bergi

Function declarations are always local to the current scope, like a variable declared with the varkeyword.

函数声明总是局部于当前作用域,就像用var关键字声明的变量一样。

However, the difference is that if they are declared(instead of assigned to a variable) their definition is hoisted, so they will be usable everywhere in the scope even if the declaration comes in the end of the code. See also var functionName = function() {} vs function functionName() {}.

但是,不同之处在于,如果它们被声明(而不是分配给变量),它们的定义会被提升,因此即使声明出现在代码的末尾,它们也可以在范围内的任何地方使用。另请参阅var functionName = function() {} 与 function functionName() {}

回答by deck

Noteworthy distinction taking implicit globals into account:

考虑到隐式全局变量的值得注意的区别:

var foo = function() {
  // Variables
  var myVar1 = 42;  // Local variable
      myVar2 = 69;  // Implicit global (no 'var')

  // Functional Expressions
  var myFn1 = function() { ... }  // Local 
      myFn2 = function() { ... }  // Implicit global

  function sayHi() {
    // I am a function declaration. Always local.
  }
}

Hopefully that clarifies a little. Implicit globals are defined if you forget a varbefore your assignment. Its a dangerous hazard that applies to variable declarations and functional expressions.

希望这能澄清一点。如果您var在分配之前忘记了 a ,则会定义隐式全局变量。它是适用于变量声明和函数表达式的危险危险。

回答by Andrew Hymanman

Your first example (var foo = function() {...}) is called an anonymous function. It is dynamically declared at runtime, and doesn't follow the same rules as a normal function, but follows the rules of variables.

您的第一个示例 ( var foo = function() {...}) 称为匿名函数。它是在运行时动态声明的,不遵循与普通函数相同的规则,而是遵循变量的规则。