简体   繁体   English

Object 数组:引用现有对象或新实例

[英]Object array: reference to existing objects or new instances

I have the following struct (or class?) in JavaScript:我在 JavaScript 中有以下结构(或 class?):

function ImageStruct() {
    this.id = -1;
    this.isCover = true;
    this.isPaired = false;
    this.row = -1;
    this.column = -1;
} 

I add this class to a bidimensional array.我将此 class 添加到二维数组中。

var imgStruct = new ImageStruct();
imgStruct.id = id;
imgStruct.row = row;
imgStruct.column = column;

$.myNameSpace.matrixPos[row][column] = imgStruct;

When I do something like this:当我做这样的事情时:

var imgStrc = $.myNameSpace.matrixPos[row][column];

If I modify imgStrc , object in $.myNameSpace.matrixPos[row][column] doesn't reflect that changes.如果我修改imgStrc$.myNameSpace.matrixPos[row][column]中的 object 不会反映该更改。

Is there any way to 'fix' this?有没有什么办法解决这一问题?

It definitely should be "by reference".它绝对应该是“通过引用”。 I set up a JsFiddle to confirm.我设置了一个 JsFiddle 来确认。

http://jsfiddle.net/bJaL4/ http://jsfiddle.net/bJaL4/

Structs aren't really possible in JavaScript. JavaScript 中的结构实际上是不可能的。 There are explicit value and reference types.有明确的值和引用类型。 All objects are treated as reference types.所有对象都被视为引用类型。

If you modify imgStrc it by changing its properties (eg by doing imgStrc.id = 42 ), that change will affect the object in $.myNameSpace.matrixPos[row][column] (as it is in fact the same object).如果您通过更改imgStrc的属性来修改它(例如通过执行imgStrc.id = 42 ),该更改影响$.myNameSpace.matrixPos[row][column]中的 object (因为它实际上是同一个对象)。

Only if you modify imgStrc by reassigning it, it won't.仅当您通过重新分配imgStrc来修改它时,它才不会。 There is no way to 'fix' this, other than setting $.myNameSpace.matrixPos[row][column] = imgStrc or wrapping the whole thing into a wrapper object (or one-element array).除了设置$.myNameSpace.matrixPos[row][column] = imgStrc或将整个东西包装到包装器 object (或单元素数组)中之外,没有办法“修复”这个问题。

You can either modify the object directly:您可以直接修改 object:

$.myNameSpace.matrixPos[row][column].someMethod();

which is admittedly not very convenient, or feed the value back into the array after modifying it:诚然这不是很方便,或者在修改后将值反馈回数组:

var tmp = $.myNameSpace.matrixPos[row][column];

tmp.someMethod(); // Do something...

$.myNameSpace.matrixPos[row][column] = tmp;

JavaScript doesn't have pointers. JavaScript 没有指针。 You last var statement just overwrites the imgStrc value with the value of the namespace property.您最后的 var 语句只是用命名空间属性的值覆盖了 imgStrc 值。 Whatever that value is is what you modified.无论该值是什么,都是您修改的值。 If it's an object, all references to that obj will see the change.如果它是 object,则对该 obj 的所有引用都会看到更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将新数组对象合并到现有对象js - Merge new array objects to existing object js 根据现有对象的属性值创建新的对象数组 object 将现有对象添加到新数组 - Create a new array of objects by property value from existing object add existing objects to new array 基于现有数组和 object 构造新的对象数组 - Construct new array of objects based on existing array and object 将新对象推送到数组会导致现有对象发生变异 - pushing new objects to array results in mutation of existing object 将新对象添加到现有对象数组jquery / javascript - Add new object to existing array of objects jquery/javascript 从现有对象创建新对象数组,不重复 - Creating a new array of objects from an existing object based no duplication 当我尝试将新的 object 添加到我的 state 数组中时,它会将所有现有对象更改为新的 object - When I try to add a new object to my state array, it changes all existing objects to the new object 从现有对象数组创建新的对象数组时出现引用错误? - Getting referrence error while creating new array of object from existing array of objects? 如何使用 useState 挂钩更新现有的对象数组(将新的 object 添加到数组中)? - How to update an existing array of objects (add a new object to the array) with the useState hook? 在 React 中将新的子对象添加到现有的 object - Add new child objects to existing object in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM