1

In Angular context I'm working with custom Objects defined with an interface, let's say "a" and "b". I'm creating those as class attributes, as well as an empty array of those.

   public a: CustomObj;
   public b: CustomObj;
   public array: CustomObj[]=[];

In the class constructor, I fill the array with a and b, at that time they have no values.

this.array = [a,b];

At some point I modify a or b (with ([ngModel]) binding), but when trying to access those objects through the Array, those are still "undefined".

Can someone explain why ?

Thanks.

  • 1
    When you say "**modify**", what exactly do you mean? `this.a = otherA` will assign a completely new reference to `a` and leave `array[0]` untouched, however, `this.a.prop = "foo"` will operate on the existing reference, so that `array[0].prop === "foo"`. – p.s.w.g Jan 30 '19 at 21:12
  • We might need a full example to see the problem you're describing – Evert Jan 30 '19 at 21:16
  • Oh... I think you got it :D I'm using ngModel binding, so I guess that's what ngModel does... Maybe I should bing directly to the instance in the array then ? – Pierre-Yves Rouillé Jan 30 '19 at 21:20
  • @Pierre-YvesRouillé I've never developed in angular, so I can't say for sure. Just hoping to point you/other readers in the right direction. But Evert is right, too, see if you can put together an [MCVE](https://stackoverflow.com/help/mcve) so we can see what's actually going on. – p.s.w.g Jan 30 '19 at 21:26
  • @p.s.w.g I think that's the answer, I tried to bind the field to array[0] and when modified, it didn't affect "a" either. It seems logic that when a value is updated by ngModel, its a new reference. I'm still weak in JS though I've been working with angular for several months, and I didn't know 'this.a = otherA' would erase the refrence, I thought it would just change the value. Anyway thanks! – Pierre-Yves Rouillé Jan 30 '19 at 21:38
  • what are `A` and `B` you modify? presumably the models that bind `a` and `b`. If that is the case, please edit your description to make it clear. In addition, javascript is `always` pass by value, do not mislead people. – ABOS Jan 31 '19 at 14:21
  • @ABOS edited my post. About passing only by value, this says otherwise :https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0 – Pierre-Yves Rouillé Jan 31 '19 at 19:26
  • @Pierre-YvesRouillé, this has been discussed on SO many times, see https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language, quote: `JavaScript does not pass by reference. If you read closely, you will realize that all contrary opinions misunderstand what is meant by pass-by-value and they falsely conclude that the ability to update an object's data via the function parameter is synonymous to "pass-by-value".` – ABOS Jan 31 '19 at 19:33
  • @ABOS well thanks for the info, I couldn't believe otherwise since when you search "javascript passing by reference" all results seems to agree on the fact it can be both. Didn't dig into it enough. Still I don't get why people are downvoting my answer since what psgw said was true and resolved my problem => a new assignation to my initial object made me lose the reference to it. Isn't it ? – Pierre-Yves Rouillé Jan 31 '19 at 20:08
  • @Pierre-YvesRouillé, I guess it is downvoted because the current answer does not have not enough details. – ABOS Jan 31 '19 at 20:11

1 Answers1

0

a and b are reference objects, they hold a reference to an instance of an object in memory. When you go

array = [a, b];

You now have an array with [undefined, undefined] because both were not pointing to an instance of an object.

Now when you go

a = {} //Some instance of an object

This does not affect the array at all as it doesn't know where the undefined came from.

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60