在 JavaScript 中使原始数据类型只读/非配置
有谁有使单个对象道具只读/不可配置的示例实现吗?我的意思是原始数据类型。曾尝试使用 ES5 Object API,但遇到了困难。
我无法显示代码,因为它仍然处于“混乱”阶段,但基本上我正在迭代一个外部对象,该对象本身包含许多对象。这些对象各自保存各种原始数据类型。我已将外部对象设置为只读、非配置等,但不知道如何对单个道具(最里面的道具)执行同样的操作。
因此,如果 outer.inner.prop === "Hello"
,我想将该值设置为 readOnly
。
谢谢!
更新
我刚刚发现了这一点,这一切都在我用来迭代 props 的 for 循环中。现在我实际上已经获得了道具的数据描述符,甚至是原始的数据描述符。 :) 谢谢大家!
Does anyone have any example implementation of making individual object props readOnly/non-configurable? I mean primitive data types. Have tried using ES5 Object API, but hitting a brick wall.
I can't show code, because it's still at that "messy" phase, but basically I'm iterating through an outside object which, itself, holds numeruos objects. Those objects each hold various primitive data types. I have made the outer objects readOnly, non-config, etc, but can't figure out how to do likewise for individual props, the innermost props.
So, if outer.inner.prop === "Hello"
, I want to make that value readOnly
.
Thanks!
UPDATE
I just figured this out, it was all in the for loop I was using to iterate over props. Now I've actually get data descriptors for the props, even the primitive ones. :) Thanks all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须迭代内部对象,因为无法使用标准 ES5 方法深度冻结对象。
编辑:
如果您不想
冻结
,也适用于defineProperty
:You have to iterate through the inner object, since there is no way to deep-freeze an object using standard ES5 methods.
Edit:
Also works for
defineProperty
if you don't want tofreeze
:我不能 100% 确定我正确理解你的问题,但从我收集的信息来看,你要求私有变量。如果是这样,可以使用闭包轻松实现。
I'm not 100% sure I understand your question correctly, but from what I gather you are asking for private variables. If so, that can be easily achieved using closures.
以下 (ES5) 示例有帮助吗?它创建一个空的构造函数,带有属性
a
的getter(并且没有setter,所以事实上a
是只读的):不使用ES5你可以这样做
但这不是100% 故障安全:
Obj.prototype.getA
可以被覆盖。Would the following (ES5) example help? It creates an empty constructor, with a getter for property
a
(and no setter, so de factoa
is read only):Not using ES5 you can do
But that is not 100% failsafe:
Obj.prototype.getA
can be overwritten.这里是一个 jsfiddle,展示了如何使用 ES5 getter/setter 定义来使对象的属性成为某种东西只能获取。代码如下所示:
当然,getter 可以从任何地方获取属性(“x”)的值,例如构造函数的闭包或其他东西。关键是设置器根本不会更改该值,因此尝试更改它:
不会有任何效果。
Here is a jsfiddle showing how you can use ES5 getter/setter definitions to make a property of an object something that can only be fetched. The code looks like this:
Of course the getter could obtain the value of the property ("x") from anywhere, like a closure from a constructor or something. The point is that the setter simply does not change the value, so attempts to change it:
will not have any effect.