JavaScript 中可以使用常量变量吗?
我在一个网站上读到,您可以在 JavaScript 中创建常量 变量,例如:
const x = 20;
但在另一个网站上我读到您不能。所以我现在很困惑现在是什么?
另外,在 Visual Studio 2010 中,当我编写 const
时,它会在 JavaScript 文件中为其添加下划线并显示语法错误。
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?
Also in Visual Studio 2010 when I write const
it underlines it in the JavaScript file and shows syntax error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
const
是 ECMAScript Harmony 的一项提议功能 (与适当的块范围let
一起,它应该取代var
和隐式全局变量)。 ECMAScript Harmony 是 ECMAScript 下一版本的创意集锦。const
也是 ECMAScript 4 的一部分。ECMAScript4 从未发布,也永远不会发布,而 ECMAScript Harmony 只会在几年后发布。因此,您无法可靠地使用它。
ECMAScript 的一些实现或衍生版本实现了 const(例如 ActionScript)。还有一些实现接受
const
作为var
的同义词(IOW,你可以使用const
,但它不会给你任何保护。)但是,除非您绝对可以保证您的代码仅在 ECMAScript 的非常特定派生的非常特定实现的非常特定版本上运行,否则最好避免它。 (这真是一个耻辱,因为
const
尤其是let
相对于var
和隐式全局变量来说是一个巨大的改进。 )const
is a proposed feature of ECMAScript Harmony (together with a properly block-scopedlet
it is supposed to replacevar
and implicit globals). ECMAScript Harmony is a grab-bag of ideas for the next versions of ECMAScript.const
was also a part of ECMAScript 4.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.
There are some implementations or derivatives of ECMAScript that implement
const
(ActionScript, for example). There are also some implementations that acceptconst
as a synonym forvar
(IOW, you can useconst
, but it won't give you any protection.)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 especiallylet
are a huge improvement overvar
and implicit globals.)如果您正在寻找一个只读变量,您可以使用类似的东西来模拟它
,然后像这样使用它
if you're looking for a read-only variable, you simulate that with something like
and then use it like
解决方案是创建一个对象并将所有常量放入该对象中:
用法:
任何修改变量的尝试都会生成错误:
The solution is to create an object and put all your constants in the object:
Usage:
Any attempt to modify the variable will generate an error:
ECMAScript 中没有 const(不考虑已失效的 4.0 版本和 ActionScript)。
const
可在 中找到JScript.NET,以及一些最新版本的 JS 引擎 例如 Firefox、Opera 9、Safari 作为供应商特定的扩展。There's no
const
in ECMAScript (disregarding the dead 4.0 version, and ActionScript).The
const
is available in JScript.NET, and some recent versions of JS engines e.g. Firefox, Opera 9, Safari as a vendor-specific extension.JavaScript ES6(重新)引入了
const 关键字
,所有主流浏览器均支持该关键字。
除此之外,
const
的行为类似于让
。它的行为与原始数据类型(布尔、空、未定义、数字、字符串、符号)的预期相同:
注意:请注意有关对象的陷阱:
JavaScript ES6 (re-)introduced the
const
keyword which is supported in all major browsers.Apart from that,
const
behaves similar tolet
.It behaves as expected for primitive datatypes (Boolean, Null, Undefined, Number, String, Symbol):
Attention: Be aware of the pitfalls regarding objects:
我相信有一些东西不属于 var ,是一个全局变量......js 中没有 const 。
var a="test"
与a="test"
不同I believe there is something which is not with var , is a global variable....there is no const in js.
var a="test"
is different froma="test"
不,JavaScript 中没有数据类型“const”。
JavaScript 是一种松散类型语言。每种变量都用 var 声明
No, there is no data type "const" in JavaScript.
JavaScript is a loosely typed language. Every kind of variable is declared with a var