你能在JavaScript中使用常量变量吗? [英] Can you use constant variables in JavaScript?

查看:147
本文介绍了你能在JavaScript中使用常量变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上读到你可以在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?

在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的一个提议功能(以及正确的块范围它应该替换 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 ,尤其是巨大的改进 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中使用常量变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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