问题描述
我在一个网站上读到,您可以在 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 中使用常量变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)