JavaScript行之间保留什么状态? [英] What state is kept between JavaScript lines?

查看:91
本文介绍了JavaScript行之间保留什么状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道提交给 babel-node 的两行JavaScript代码之间的状态是什么。我的困惑是因为如果你写两行代码,你可以覆盖一个变量定义而不会有错误。例如,使用 babel-node --presets es2015 ,您可以执行以下操作:

 > const a = 1; 
undefined
>让a = 2;
undefined

现在如果你写一行,你会得到一个错误: p>

 > const a = 1;让a = 2; 
TypeError:repl:重复声明a
...

它似乎在第一种情况下, a 的状态被定义为 1 const 变量赋值)丢失(直到第二个赋值为止),而在第二种情况下,它将被维护。



?并且维护哪些状态?

解决方案

因为 const let 是新的语法,它们必须被折叠到ES6之前可用的唯一绑定机制: var 。在这种情况下, var 允许各种随意的重新分配,而不会产生任何类型的警告。



所以当你键入在 babel-node 中的表达式中,babel将其分解,然后显示结果。 Babel可以在透明时间检查是否滥用 const 绑定,这就是为什么你看到 const a = 1的错误;让a = 2 。但是,当作为单独表达式进行泛化/评估时, const a = 1 let a = 2 将不会显示错误因为宝贝不能单独检测到任何一个问题。






更可视化的问题演示:对于每一个表达式 expr 您输入 babel-node REPL,这实际上是发生了什么

  evaluate(transpile(expr))
// => someResult

所以你不会在这里看到错误



pre> 评估(transpile('const a = 1'))
评估('var a = 1')
// bind a to 1
// return undefined

evaluate(transpile('let a = 2'))
evaluate('var a = 2')
// bind a to 2
// return undefined

但是你会在这里看到一个错误

  evaluate(transpile('const a = 1; let a = 2'))
//在transile期间发生错误:const a已被声明


I was wondering what states are kept between two lines of JavaScript code submitted to babel-node. My confusion arises because if you write two lines of code, you can override an variable definition without an error. For example, with babel-node --presets es2015, you can do:

> const a = 1;
undefined
> let a = 2;
undefined

Now if you write it in one line, you get an error:

> const a = 1; let a = 2;
TypeError: repl: Duplicate declaration "a"
...

It seems that in the first case, the state that a is defined as 1 (const variable assignment) is lost (yet not until the second assignment), while in the second case, it is maintained.

What causes the differences here? and which states are maintained?

解决方案

Because const and let are new syntaxes, they must be transpiled to the only binding mechanism that was available before ES6: var. In which case, var allows all sorts of haphazard reassignment without generating any kind of warning.

So when you type an expression in babel-node, babel transpiles it, evaluates it, then displays the result. Babel can check for misuse of a const binding at transpile time, which is why you're seeing the error for const a = 1; let a = 2. But const a = 1 and let a = 2, when transpiled/evaluated as separate expressions, will not exhibit the error because babel is not able to detect a problem in either expression alone.


A more visual demonstration of the issue: For every expression expr you type in the babel-node REPL, this is essentially what's happening

evaluate(transpile(expr))
// => someResult

So you won't see an error here

evaluate(transpile('const a = 1'))
evaluate('var a = 1')
// bind a to 1
// return undefined

evaluate(transpile('let a = 2'))
evaluate('var a = 2')
// bind a to 2
// return undefined

But you will see an error here

evaluate(transpile('const a = 1; let a = 2'))
// ERROR during transpile: const a has already been declared

这篇关于JavaScript行之间保留什么状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆