我在一个网站上读到,您可以在 JavaScript 中创建 constant 变量,例如:
I read on one site that you can make constant variables in JavaScript like:
const x = 20;
但在另一个网站上我读到你不能.所以我现在很困惑,现在是什么?
but on another site I read that you can't. So I am confused now what is it now?
同样在 Visual Studio 2010 中,当我编写 const
时,它会在 JavaScript 文件中加下划线并显示语法错误.
Also in Visual Studio 2010 when I write const
it underlines it in the JavaScript file and shows syntax error.
const
是 ECMAScript Harmony 的一项提议功能(与正确的块范围 let
一起,它应该替换 var
和隐式全局变量).ECMAScript Harmony 是 ECMAScript 下一版本的灵感来源.
const
is a proposed feature of ECMAScript Harmony (together with a properly block-scoped let
it is supposed to replace var
and implicit globals). ECMAScript Harmony is a grab-bag of ideas for the next versions of ECMAScript.
const
也是 ECMAScript 4 的一部分.
const
was also a part of ECMAScript 4.
ECMAScript 4 从未发布过,也永远不会发布,ECMAScript Harmony 只会在几年后发布.因此,您不能可靠地使用它.
ECMAScript 4 was never released and never will be, and ECMAScript Harmony will only be released in a couple of years. Therefore, you cannot reliably use it.
ECMAScript 的一些实现或派生类实现了 const
(例如 ActionScript).还有一些实现接受 const
作为 var
的同义词(IOW,你可以使用 const
,但它不会给你任何保护.)
There are some implementations or derivatives of ECMAScript that implement const
(ActionScript, for example). There are also some implementations that accept const
as a synonym for var
(IOW, you can use const
, but it won't give you any protection.)
但是,除非您绝对可以保证您的代码只能在非常特定的 ECMAScript 衍生版本的非常特定的实现的非常特定的版本上运行,否则最好避免使用它.(这真是太可惜了,因为 const
尤其是 let
是对 var
和隐式全局变量的巨大 改进.)
However, unless you absolutely can guarantee that your code will only run on very specific versions of very specific implementations of very specific derivatives of ECMAScript, it's probably better to avoid it. (Which is a real shame, because const
and especially let
are a huge improvement over var
and implicit globals.)
这篇关于你可以在 JavaScript 中使用常量变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!