javascript - JavaScript 中的 const 关键字 :

标签 javascript global-variables constants

今天(2014 年)是否建议使用 const 关键字在 JavaScript 中声明常量?还存在浏览器兼容性问题吗?另外,有没有办法声明一个全局常量?

注意:这个问题不是重复的,我问的是 2014 年。Stack Overflow 中的其他帖子可以追溯到 2008-2012 年

最佳答案

截至 2014 年 6 月,MDN 是这样说的:

The current implementation of const is a Mozilla-specific extension and is not part of ECMAScript 5. It is supported in Firefox & Chrome (V8). As of Safari 5.1.7 and Opera 12.00, if you define a variable with const in these browsers, you can still change its value later. It is not supported in Internet Explorer 6-10, but is included in Internet Explorer 11. The const keyword currently declares the constant in the function scope (like variables declared with var).

Firefox, at least since version 13, throws a TypeError if you redeclare a constant. None of the major browsers produce any notices or errors if you assign another value to a constant. The return value of such an operation is that of the new value assigned, but the reassignment is unsuccessful only in Firefox and Chrome (at least since version 20).

const is going to be defined by ECMAScript 6, but with different semantics. Similar to variables declared with the let statement, constants declared with const will be block-scoped.

引用here

关于javascript - JavaScript 中的 const 关键字 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24370447/

相关文章:

javascript - 为什么 2 个 immutable.js 列表有很大不同?

javascript - 键盘组合快捷键

javascript - 使用 JavaScript 更改 onclick 事件不起作用

c++ - const成员函数如何改变对象的数据?

.net - 为什么 .Net 中的常量不是全部大写?

c++ - 为什么 std::basic_ostream::operator<< 不是 const 限定的?

javascript - JS 字符串/数字和垃圾回收

c++ - 为什么将全局变量和静态变量初始化为其默认值?

c - 多个共享库使用的变量值

javascript - 午睡的全局功能