简体   繁体   English

如何确定是否可以将新字段添加到现有对象?

[英]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.

相关问题 添加新的“自定义过滤器”后,解决查询字段的现有选择 - Resolving pre-existing selection for a Lookup Field after a new Custom Filter has been added 如何在模式中未存在的MongoDB中的现有文档中定义新字段? (Node.js) - How to define a new field in a pre-existing document in MongoDB that isn't in the schema? (Node.js) 我如何在D3中选择一个预先存在的元素 - How can i select a pre-existing element in d3 jQuery getJSON-引用预先存在的对象 - jQuery getJSON - reference pre-existing object 如何构造这种新逻辑以适合现有代码? - How do I structure this new logic to fit into pre-existing code? 从预先存在的字段生成ISO日期 - generate an ISO date from a pre-existing field 如何在不使用write()的情况下将iframe文档替换为另一个现有的文档对象 - How to replace iframe document with another pre-existing document object, without using write() 带有倾斜边框的现有div内的新div - New divs inside pre-existing div with slanted borders Knockout.js观察预先存在的API对象的成员 - Knockout.js Observing members of a pre-existing API object IndexedDB:修改对象存储中先前存在的对象 - IndexedDB: Modifying pre-existing objects in an object store
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM