gpt4 book ai didi

javascript - JavaScript 中的 const 关键字 :

转载 作者:行者123 更新时间:2023-11-28 19:45:20 26 4
gpt4 key购买 nike

今天(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com