简体   繁体   English

提升名称解析顺序在JavaScript中如何工作?

[英]How hoisting name resolution order works in JavaScript?

I came across a interesting quiz 我遇到了一个有趣的测验

function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = '11';
}
alert(typeof bar());

My interpretation is like this (Which is wrong according to console :) ): 我的解释是这样的(根据控制台,这是错误的:)):

var foo; // global variable
function bar(){
    function foo(){}
    var foo;  //  Here variable foo should override foo function 
    return foo; // (according to me foo should be variable with undefined value) What is going on here, How JavaScript resolve naming order ?
    foo = 10;
    foo = "11";
}

Here is a reference which I am reading this 这是我读的引用

In JavaScript, a name enters a scope in one of four basic ways: 1.Language-defined: All scopes are, by default, given the names this and arguments. 在JavaScript中,名称通过以下四种基本方式之一输入范围:1.Language-defined:默认情况下,所有范围都被赋予名称this和arguments。 2. Formal parameters: Functions can have named formal parameters, which are scoped to the body of that function. 2.形式参数:函数可以具有命名形式参数,其范围仅限于该函数的主体。 3. Function declarations: These are of the form function foo() {}. 3.函数声明:这些函数的形式为foo(){}。 4. Variable declarations: These take the form var foo; 4.变量声明:这些变量声明采用var foo;的形式。

He later quoted : 他后来引用:

The most important special case to keep in mind is name resolution order. 要记住的最重要的特殊情况是名称解析顺序。 Remember that there are four ways for names to enter a given scope. 请记住,名称输入给定范围的方式有四种。 The order I listed them above is the order they are resolved in. In general, if a name has already been defined, it is never overridden by another property of the same name. 我在上面列出的顺序是它们解决的顺序。通常,如果已经定义了一个名称,则它永远不会被同名的另一个属性覆盖。 This means that a function declaration takes priority over a variable declaration. 这意味着函数声明优先于变量声明。 This does not mean that an assignment to that name will not work, just that the declaration portion will be ignored. 这并不意味着对该名称的分配将不起作用,只是声明部分将被忽略。

Which is confusing for me, Can anyone simplify this with reference to above example ? 这让我感到困惑, 任何人都可以参考上面的示例来简化它吗? Main point I want to know : 我想知道的要点:

  • How variables without var inside a function hoisted ? 如何在函数内部没有var变量上吊起?
  • Does variable overriding occurs during hoisting ? 提升过程中是否发生变量超控?

Lets leave the function foo for the moment. 现在暂时让函数foo离开。 Within a function, if a variable is declared anywhere inside that function, the declaration will be moved to the top of the function. 在函数内,如果在函数内的任何地方声明了变量,则声明将移至函数的顶部。 So, it is actually evaluated like this 因此,实际上是这样评估的

function bar() {
    var foo;
    return foo;
    foo = 10;
    foo = '11';
}

But when you have a function declared by the same name, it will take precedence and it will be evaluated similar to this 但是,当您使用相同的名称声明函数时,它将具有优先权,并且其计算结果与此类似

function bar() {
    var foo = function() {};
    return foo;
    foo = 10;
    foo = '11';
}

That is why you are getting function in the alert box. 这就是为什么您在警报框中获得function原因。

Any variable without var inside a function becomes a global variable by default. 默认情况下,函数内部没有var任何变量都将成为全局变量。

when you have a function declaration inside another function(like in your example), it gets hoisted first followed by the variable declarations. 当您在另一个函数中有一个函数声明时(例如您的示例中),首先将其提升,然后是变量声明。

examples to demonstrate variable overriding. 演示变量覆盖的示例。

function bar() {
    var foo = 10;
    function foo() {}
    return foo;
}

bar(); //--> returns 10;

function bar() {
   var foo;
   function foo() {}
   return foo;
}

bar(); //--> returns the function object foo.

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

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