简体   繁体   English

是否可以在javascript中重新分配const变量

[英]Is it possible to reassign const variable in javascript

Is it possible in JavaScript to reassign const variable;是否可以在 JavaScript 中重新分配 const 变量;
In C++ we can cast variable to and from const, but is it something similar possible in JavaScript;在 C++ 中,我们可以将变量转换为 const 或从 const 转换,但在 JavaScript 中是否有类似的可能?

Here I ask about我在这里问

const a = 1;
unconst(a);
a = "xyz";
a === "xyz" // true

Not object properties reassignments and array push/pop;不是对象属性重新分配和数组推送/弹出;
or或者

let a = 1;
const(a);
a = "xyz"; // error 
a === 1 // true

a is const now, something like Object.freeze for objects a 现在是 const,类似于 Object.freeze 的对象

No, not with standard API.不,不是标准 API。 See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const另见: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

Constants are block-scoped, much like variables defined using the let statement.常量是块作用域的,很像使用 let 语句定义的变量。 The value of a constant can't be changed through reassignment, and it can't be redeclared.常量的值不能通过重新赋值来改变,也不能重新声明。

The const declaration creates a read-only reference to a value. const 声明创建一个对值的只读引用。 It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned.这并不意味着它持有的值是不可变的——只是变量标识符不能被重新分配。 For instance, in the case where the content is an object, this means the object's contents (eg, its properties) can be altered.例如,在内容是对象的情况下,这意味着可以更改对象的内容(例如,其属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM