[英]How to be sure if a new field can be added to the pre-existing object or not?
I'm a C/Python
guy, shifted to 'Javascript' recently. 我是
C/Python
,最近改用了“ Javascript”。
Basically, I receive an object (named context
) in a function as an argument.The caller function's definition is hidden to me. 基本上,我在函数中收到一个对象(名为
context
)作为参数。调用者函数的定义对我来说是隐藏的。 Now I need to add a field( type
) to the context
. 现在,我需要在
context
添加一个field( type
)。 When I add type
to context directly, it doesn't reflect changes in 'context'. 当我直接向上下文添加
type
,它不能反映“上下文”中的更改。 But when I try to modify the value of one of the existing field of 'context', this change is reflected. 但是,当我尝试修改“上下文”的现有字段之一的值时,就会体现出这种变化。
Then I create one more object( new_obj
) and copy 'context' in the 'new_object', then tried to add the field in the 'new_object', but unsuccessful. 然后,我再创建一个对象(
new_obj
)并在“ new_object”中复制“ context”,然后尝试将字段添加到“ new_object”中,但未成功。 So from this behaviour, I guess the value is not copied but the reference is. 因此,从这种行为来看,我猜该值不会被复制,而引用会被复制。
So finally I need to copy field by field in the new_obj
and then add the new field 'type'. 所以最后我需要在
new_obj
逐字段复制字段,然后添加新的字段“ type”。 Also if I create a local object, then new fields are being added and object structure is modified. 另外,如果我创建一个本地对象,则将添加新字段并修改对象结构。
So far so good. 到现在为止还挺好。 But I was wondering about the implementation of the 'context' object in background caller function.
但是我想知道后台调用者函数中“上下文”对象的实现。 I mean if there is some 'const' type thing here(as in C, (Random thoughts :P)), then at what level it is applied ie there is only restriction of adding new fields or also of changing values of the existing fields.
我的意思是,如果这里有一些'const'类型的东西(如C,(随机想法:P)),那么它将应用到什么级别,即,仅存在添加新字段或更改现有字段的值的限制。 。 Needed some light over this issue.
需要对此问题有所了解。
But I was wondering about the implementation of the 'context' object in background caller function.
但是我想知道后台调用者函数中“上下文”对象的实现。 I mean if there is some 'const' type thing here(as in C, (Random thoughts :P)), then at what level it is applied ie there is only restriction of adding new fields or also of changing values of the existing fields.
我的意思是,如果这里有一些'const'类型的东西(如C,(随机想法:P)),那么它将应用到什么级别,即,仅存在添加新字段或更改现有字段的值的限制。 。 Needed some light over this issue.
需要对此问题有所了解。
It depends partially on whether the object is a host-provided object (eg, from the browser or similar), or a true JavaScript object. 它部分取决于对象是主机提供的对象 (例如,来自浏览器或类似设备的对象),还是真正的JavaScript对象。
A host-provided object can do very nearly anything it wants. 主机提供的对象几乎可以完成任何所需的操作。 :-)
:-)
A true JavaScript object can be "sealed" via Object.seal
. 可以通过
Object.seal
“密封”真正的JavaScript对象。 That prevents new properties being added to it, but doesn't prevent changes to existing properties — exactly matching your description of context
. 这样可以防止向其添加新属性,但不能阻止对现有属性的更改-与您对
context
的描述完全匹配。 Here's an example: 这是一个例子:
var o = Object.seal({ answer: 0 }); console.log(o.answer); // 0 o.answer = 42; console.log(o.answer); // 42 o.question = "Life, the Universe, and Everything"; console.log(o.question); // undefined
If you use strict mode , trying to create a property on a sealed object is a handy error: 如果使用严格模式 ,尝试在密封对象上创建属性是一个方便的错误:
"use strict"; var o = Object.seal({ answer: 0 }); console.log(o.answer); // 0 o.answer = 42; console.log(o.answer); // 42 o.question = "Life, the Universe, and Everything"; // Throws error console.log(o.question); // (we don't get here)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.