[英]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. 用
var
和function
声明的变量具有函数作用域,而不是块作用域。 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
let
或const
关键字声明变量,则它们具有块作用域且不会被吊起。
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.