Javascript 中的 const 关键字范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12272836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:46:47  来源:igfitidea点击:

const keyword scope in Javascript

javascriptconst

提问by JohnJohnGa

1. >>> const a = 2
2. >>> var a = 3
3. >>> a = 4
4. >>> a // print 2

Why the operation line 3 is allowed? const seems more "global" than without any keyword...

为什么操作线3是允许的?const 似乎比没有任何关键字更“全局”......

采纳答案by JohnJohnGa

This is is just how constworks(or doesn't work):

是多么const的作品(或不工作):

Creates a constant1that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables [.. and cannot share a name] with a function or a variable in the same scope.

Firefox [..] throws a TypeError if you redeclare2[which is different than re-assigning] a constant. None of the major browsers produce any notices or errors2,3if you assignanother value to a constant[..] but the reassignment is unsuccessful (only) in Firefox and Chrome (at least since version 20).

创建一个常量1,它可以是声明它的函数的全局或局部的。常量遵循与变量相同的范围规则 [.. 并且不能与同一范围内的函数或变量共享名称]。

如果您重新声明2[不同于重新分配] 常量,Firefox [..] 会抛出 TypeError 。如果您为常量[..]分配另一个值,任何主要浏览器都不会产生任何通知或错误2,3但重新分配在 Firefox 和 Chrome 中(仅)不成功(至少从版本 20 开始)。

Note that constis notpart of the ECMAScript 5 specification and the JavaScript 1.5 semantics will be re-definedin ECMAScript 6.

请注意,const不是ECMAScript 5 规范的一部分,JavaScript 1.5 语义将在 ECMAScript 6 中重新定义

Behavior willvary across browser implementations with respect to support and re-declaration/re-assignments semantics.

在支持和重新声明/重新分配语义方面,行为因浏览器实现而异。



1In IE 9, using const a = 2results in

1在 IE 9 中,使用const a = 2结果

"Syntax error"

“语法错误”

2In FF 14, const a = 2; var a = 3; a = 4; a, when evaluated as a single program, results in

2在 FF 14 中const a = 2; var a = 3; a = 4; a,当作为单个程序进行评估时,结果为

TypeError: redeclaration of const a

类型错误:重新声明 const a

which is differentthan executing each line one-at-a-time in the REPL. I suspectthis is because varis hoisted abovethe constand because a const "cannot share a name with a function or variable in the same scope".

在 REPL 中一次执行每一行不同。我怀疑这是因为它var挂在上面const并且因为一个常量“不能与同一范围内的函数或变量共享一个名称”。

3In Chrome 21, const a = 2; var a = 3; a = 4; aevaluates to 2 with no warning or message.

3在 Chrome 21 中,const a = 2; var a = 3; a = 4; a评估为 2,没有警告或消息。

回答by Mercury

constscope is defined as 'block scoped'(the scope of which, is restricted to the block in which it is declared).

const范围被定义为“块范围”(其范围仅限于声明它的块)。


MDN documentation:


MDN 文档

Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through re-assignment, and it can't be redeclared.

常量是块作用域的,很像使用 let 语句定义的变量。常量的值不能通过重新赋值而改变,也不能重新声明。

Regarding your specific issue: First as comments said constis relevant in ES6. I don't know about you but i get (typing your line 2: var a = 3;): SyntaxError: Identifier 'a' has already been declared so your example is not quite possible.

关于您的具体问题:首先,正如评论所说const,在 ES6 中是相关的。我不了解你,但我得到(输入你的第 2 行var a = 3;:):SyntaxError:标识符 'a' 已经被声明,所以你的例子不太可能。