JavaScript 中可以使用常量变量吗?

发布于 2024-09-01 19:44:30 字数 226 浏览 6 评论 0原文

我在一个网站上读到,您可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

少女七分熟 2024-09-08 19:44:30

const 是 ECMAScript Harmony 的一项提议功能 (与适当的块范围 let 一起,它应该取代 var 和隐式全局变量)。 ECMAScript Harmony 是 ECMAScript 下一版本的创意集锦。

const 也是 ECMAScript 4 的一部分。ECMAScript

4 从未发布,也永远不会发布,而 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-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 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 accept const as a synonym for var (IOW, you can use const, 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 especially let are a huge improvement over var and implicit globals.)

饮惑 2024-09-08 19:44:30

如果您正在寻找一个只读变量,您可以使用类似的东西来模拟它

var constants = new (function() {
    var x = 20;
    this.getX = function() { return x; };
})();

,然后像这样使用它

constants.getX()

if you're looking for a read-only variable, you simulate that with something like

var constants = new (function() {
    var x = 20;
    this.getX = function() { return x; };
})();

and then use it like

constants.getX()
江城子 2024-09-08 19:44:30

解决方案是创建一个对象并将所有常量放入该对象中:

const={};

const.x=20;

const.y=30;

Object.freeze(const);  // finally freeze the object

用法:

var z=const.x + const.y;

任何修改变量的尝试都会生成错误:

const.x=100;  <== raises error

The solution is to create an object and put all your constants in the object:

const={};

const.x=20;

const.y=30;

Object.freeze(const);  // finally freeze the object

Usage:

var z=const.x + const.y;

Any attempt to modify the variable will generate an error:

const.x=100;  <== raises error
绅刃 2024-09-08 19:44:30

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.

别再吹冷风 2024-09-08 19:44:30

JavaScript ES6(重新)引入了 const 关键字所有主流浏览器均支持该关键字。

通过 const 声明的变量不能重新声明或重新分配。

除此之外,const 的行为类似于

它的行为与原始数据类型(布尔、空、未定义、数字、字符串、符号)的预期相同:

const x = 1;
x = 2;
console.log(x); // 1 ...as expected, re-assigning fails

注意:请注意有关对象的陷阱:

const o = {x: 1};
o = {x: 2};
console.log(o); // {x: 1} ...as expected, re-assigning fails

o.x = 2;
console.log(o); // {x: 2} !!! const does not make objects immutable!

const a = [];
a = [1];
console.log(a); // 1 ...as expected, re-assigning fails

a.push(1);
console.log(a); // [1] !!! const does not make objects immutable

JavaScript ES6 (re-)introduced the const keyword which is supported in all major browsers.

Variables declared via const cannot be re-declared or re-assigned.

Apart from that, const behaves similar to let.

It behaves as expected for primitive datatypes (Boolean, Null, Undefined, Number, String, Symbol):

const x = 1;
x = 2;
console.log(x); // 1 ...as expected, re-assigning fails

Attention: Be aware of the pitfalls regarding objects:

const o = {x: 1};
o = {x: 2};
console.log(o); // {x: 1} ...as expected, re-assigning fails

o.x = 2;
console.log(o); // {x: 2} !!! const does not make objects immutable!

const a = [];
a = [1];
console.log(a); // 1 ...as expected, re-assigning fails

a.push(1);
console.log(a); // [1] !!! const does not make objects immutable
骄傲 2024-09-08 19:44:30

我相信有一些东西不属于 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 from a="test"

非要怀念 2024-09-08 19:44:30

不,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文