创建任何对象的只读/不可变副本(包括深层属性)

编程入门 行业动态 更新时间:2024-10-24 06:26:40
本文介绍了创建任何对象的只读/不可变副本(包括深层属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在JavaScript中创建其属性无法更改的对象的只读/不可变版本?这也应适用于任何子对象的属性,等等。

How can I create a read-only/immutable version of an object in JavaScript, whose properties cannot be changed? This this should also apply to the properties of any sub objects and so on.

我遇到的所有方法都可以做到这一点( Object.defineProperty , Object.freeze 等)仅适用于对象的顶级属性,而不适用于子对象。

All methods I have come across to do this (Object.defineProperty, Object.freeze, etc) work only for the top level properties of the object, but not for the sub-objects.

(一个可能的用例:在创建/修改设置或配置在特定模块中键入对象,则需要以不可变的形式将其公开给程序的其余模块。)

(A possible use case: After creating/modifying a settings or configuration type object in a specific module, you need to expose it to the rest of the program's modules in an immutable form.)

推荐答案

这是我经过一番思考后想到的解决方案。可以很好地满足我的需求,所以我想分享一下QnA风格。 如果发现有任何改进/问题,请提出建议。

This is the solution I came up with after some thought. Works well for my needs so I thought I'd share it QnA style. Do suggest any improvements/issues if you you find them.

/** * Make the the specified object (deeply) immutable or "read-only", so that none of its * properties (or sub-properties) can be modified. The converted object is returned. * @param {object} obj Input object */ makeImmutable: function makeImmutable (obj) { if ((typeof obj === "object" && obj !== null) || (Array.isArray? Array.isArray(obj): obj instanceof Array) || (typeof obj === "function")) { Object.freeze(obj); for (var key in obj) { if (obj.hasOwnProperty(key)) { makeImmutable(obj[key]); } } } return obj; }

编辑:简化了代码。现在也可以正确处理数组。

Simplified the code. Also handles arrays correctly now.

更多推荐

创建任何对象的只读/不可变副本(包括深层属性)

本文发布于:2023-10-19 14:48:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507838.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:副本   属性   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!