简体   繁体   English

Javascript引擎如何处理条件语句和循环中的代码?

[英]How does Javascript engine deal with code inside conditional statements and loops?

I know Javascript create Global Execution Context and put them on the Execution stack and go through two stages, Creation stage, and Execution stage, on the second stage when it finds a function being invoked it creates an another execution context and put that execution context on the top of the execution stack, and repeats the same thing, once the code in the function finishes executing, it popped off the top of stack, return to execution context below in the stack. 我知道Javascript创建全局执行上下文并将其放在执行堆栈上,并经历两个阶段,即创建阶段和执行阶段,在第二阶段,当第二阶段找到要调用的函数时,它将创建另一个执行上下文并将该执行上下文放在执行堆栈的顶部,并重复同样的事情,一旦函数中的代码完成执行,它就会从堆栈顶部弹出,返回到堆栈下面的执行上下文。 But my question is that how does Javascript deal with code blocks of condition statements or loops? 但是我的问题是,Javascript如何处理条件语句或循环的代码块?

if (true) {
  //this is not the right way to write code.

  var var1 = 1;
  var var2 = 2;
  var3 = 3;

  function someFunction(arg1, arg2) {
    //code
  }
  someFunction('value1', 'value2');
}

Does the Javascript engine do the same thing when it enters a code block of condition statements or loops? 当Javascript引擎输入条件语句或循环的代码块时,它是否会做同样的事情? what actually it does? 实际上是什么呢?

Variables declared with var and function have function scope, not block scope. varfunction声明的变量具有函数作用域,而不是块作用域。 So if you write: 因此,如果您写:

function foo() {
    if (true) {
      //this is not the right way to write code.

      var var1 = 1;
      var var2 = 2;
      var3 = 3;

      function someFunction(arg1, arg2) {
        //code
      }
      someFunction('value1', 'value2');
    }
}

the variable declarations are hoisted to the top of the function. 变量声明被提升到函数的顶部。 But the assignments are still done where the appear in the flow of the code. 但是分配仍然在代码流中出现的地方完成。 It's as if you'd written. 就像你写的一样。

function foo() {
    var var1, var2, someFunction;
    if (true) {
      //this is not the right way to write code.

      var1 = 1;
      var2 = 2;
      var3 = 3;

      someFunction = function(arg1, arg2) {
        //code
      }
      someFunction('value1', 'value2');
    }
}

However, if you declare variables with the EcmaScript-6 let or const keywords, they have block scope and aren't hoisted. 但是,如果使用EcmaScript-6 letconst关键字声明变量,则它们具有块作用域且不会被吊起。

But my question is that how does Javascript deal with code blocks of condition statements or loops? 但是我的问题是,Javascript如何处理条件语句或循环的代码块?

Traditionally, these blocks would not get their own scopes, which means that you cannot declare local variables in them (they would instead be hoisted up to become function-level). 传统上,这些块不会获得它们自己的作用域,这意味着您无法在它们中声明局部变量(它们将被提升为功能级别)。

That behaviour is very unexpected for people coming from other programming languages, and has led to bugs without end. 对于来自其他编程语言的人们来说,这种行为是非常出乎意料的,并导致了无休止的错误。

To mitigate that, there is now (ECMAScript 6) a let keyword that allows you to declare variables local to individual code blocks. 为了减轻这种情况,现在有了(ECMAScript 6) let关键字 ,该关键字使您可以声明各个代码块本地的变量。 But keep in mind that a var will still work the same way it always has. 但是请记住, var仍然可以像以前一样工作。

If what you said code block is a kind of { } in C or C++ etc, javascript has no such one or has different one. 如果您说的code block是C或C ++等中的{ }类型,则javascript没有这样的代码或有不同的代码。

In javascript, it is called scope chain . 在javascript中,它称为scope chain Maybe other languages also concept about it. 也许其他语言也对此有所概念。

And Javascript only make scope chain on 而Javascript仅使scope chain处于

1) function () { } 1) 函数(){}

2) with () { } 2) 与(){}

3) catch () { } 3) 抓住(){}

For example, the most famous example is following. 例如,最著名的例子如下。

for (var i = 0; i < 5; i++) { 
    // setClickEventToDiv(i)
}

If we click any div, has event by setClickEventToDiv(i) , will have 5 since scope chain . 如果我们单击任何div,则通过setClickEventToDiv(i)具有事件,因为scope chain将有5事件。

So, fix it by 所以,通过修复它

for (var i = 0; i < 5; i++) {
    (function (index) { 
        //setClickEventToDiv(index) 
    }(i));
}

since function () {} has own scope 由于function () {}具有自己的scope

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

相关问题 javascript的条件语句里面的代码,如何在稍后的时间执行setInterval? - How to execute setInterval at a later time in the code inside conditional statements in javascript? 如何处理JavaScript条件语句中的可为空的对象 - How do I deal with nullable objects in JavaScript conditional statements 如何在循环中处理异步javascript? - How to deal with asyncronous javascript in loops? 我应该如何处理嵌套条件语句? - How should I deal with nested conditional statements? 如何在JavaScript函数中的html中插入条件语句? - How to insert conditional statements in html inside a JavaScript function? 如何在 javascript function 中返回 html 代码,使用 Z99938282F0410716EF422 onFCF42 的条件语句 - How to return html code in a javascript function with conditional statements with select onchange 如何使用条件语句在 javascript 函数中返回 html 代码 - How to return html code in a javascript function with conditional statements 在JavaScript中的条件语句中使用复选框值 - Using checkbox value inside conditional statements in JavaScript 这段代码如何作为条件语句工作,为什么没有任何“if”或“else if”语句? - How does this code work as conditional statements, why aren't there any "if" or "else if" statements? 如何处理带有条件字段的 javascript 表单中的安全性 - How to deal with security in a javascript form with conditional fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM