简体   繁体   English

Javascript 吊装代码片段

[英]Javascript hoisting code snippet

I am going through code snippets of JS hoisting.我正在浏览 JS 提升的代码片段。 One of the snippet looks like其中一个片段看起来像

var employeeId = 'abc123';

function foo() {
    employeeId();
    return;

    function employeeId() {
        console.log(typeof employeeId);
    }
}
foo(); 

The output would be: function output 将是: function

I have read about hoisting and as per my understanding all the variables would be treated as if they are declared at the top of the function and initialised at the line of their actual declaration/definition.我已经阅读了有关提升的信息,并且根据我的理解,所有变量都将被视为在 function 的顶部声明并在其实际声明/定义的行中初始化。 In this case the employeeId function identifier would be declared at the top of the function as var employeeId whose value would obviously be undefined and thus the very first line of the function should throw the error.在这种情况下, employeeId function 标识符将在 function 的顶部声明为var employeeId ,其值显然undefined ,因此 function 的第一行应该抛出错误。

Please let me know why the output is function ?请让我知道为什么 output 是function

Both var declarations and function declarations are hoisted to the top of the scope in which they occur (in that order); var声明和函数声明都被提升到它们出现的作用域的顶部(按这个顺序); var s get undefined as their value, the bindings for functions get the function as their value. var s 获得undefined作为它们的值,函数的绑定将函数作为它们的值。 Only after that's done is any step-by-step code in the function executed.只有在完成之后,才会执行函数中的任何分步代码。

So your example is effectively the same as this:因此,您的示例实际上与此相同:

var employeeId;                           // Declaration
function foo() {                          // Declaration
    function employeeId() {               // Declaration (shadows outer `employeeId`)
        console.log(typeof employeeId);
    }

    employeeId();
    return;
}
employeeId = 'abc123';
foo(); 

thus the very first line of the function should throw the error.因此函数的第一行应该抛出错误。

No , function declarations are hoisted along with the value (unless it is declared inside an inner block).函数声明与值一起提升(除非它在内部块中声明)。

In your code,在您的代码中,

var employeeId = 'abc123';

function foo() {
    console.log(employeeId); //will print function body
    return;
      function employeeId() {
        console.log(typeof employeeId);
      }
}

but if the function declaration is inside an inner block但如果函数声明在内部块内

var employeeId = 'abc123';

function foo() {
    console.log(employeeId); //will print undefined
    {
      function employeeId() {
        console.log(typeof employeeId);
      }
    }
    return;
}

This happens function declarations are hoisted before variables.发生这种情况 function 声明在变量之前被提升。

In JavaScript function and variable declarations are hoisted at the top of the execution context.在 JavaScript function 和变量声明被提升到执行上下文的顶部。 You need to remember these three rules for on the same.您需要记住这三个规则。

  1. Variable and function declaration are hoisted at the top of the execution context.变量和 function 声明被提升到执行上下文的顶部。
  2. Only declaration gets hoisted, assignments does not get hoisted.只有声明被提升,分配不会被提升。 Assignment happens where variable was declared.赋值发生在声明变量的地方。
  3. function declaration has a precedence over variable declaration in the hoisting. function声明在提升中优先于变量声明。 在此处输入图像描述

https://www.codingame.com/playgrounds/7974/understanding-hoisting-in-javascript https://www.codingame.com/playgrounds/7974/understanding-hoisting-in-javascript

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

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