本文介绍了JavaScript const关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! JavaScript中的 const 关键字是否创建了对不可变数据结构的不可变引用? [我假设JavaScript中存在不可变数据结构。]Does the const keyword in JavaScript create an immutable reference to immutable data structures? [I'm assuming that immutable data structures exist in JavaScript.]对于字符串,它似乎是这样做的:For string it appears to do so:var x = "asdf";const constantX = x;alert("before mutation: " + constantX);x = "mutated"alert("after mutation: " + constantX);输出::asdf http://jsfiddle.net/hVJ2a/推荐答案是的,它确实创建了一个不可变引用,但它没有标准化,并且在所有浏览器中都不受支持。Yes, it does create an immutable reference, but it is not been standardized and is not supported in all browsers.请参阅关于 const 关键字了解详细信息和兼容性。See this MDN article on the const keyword for details and compatability.但是,它没有做任何事情来将引用的数据结构设置为不可变。以下几个答案使用例如 Object.freezeHowever, it doesn't do anything to set the referenced data structure as immutable. Several answers below address that question using, for example, Object.freeze 这篇关于JavaScript const关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 22:56