A Simple Discrete-Event Simulation: Part 24

While making the changes I described yesterday I ran into a problem where a long chain of (what I expected to be) references didn’t return the results I was expecting. Somewhere in the chain of references something appears to be getting assigned by value.

In order to isolate the chicanery I built up the following test code, which parallels the structure I created when I included the displayGroup object in a model component, specifically the arrivals component. The displayGroup object contains an array of displayValue objects. What I see is that none of the indirections work as expected. If the initial value of a referenced item is changed then the change is not always reflected in the chain of references. I was originally fooled into thinking it was working because I changed the value of randomFred and then called the setFred method of the randomObject, which actually reassigned the value. If I change the value of randomFred and don’t call the setFred method then the pass-through doesn’t work.

I would know how to control this in a C-style language but JavaScript doesn’t give the user this type of control. It does things the way it does things, which in this case means that primitive values (like numbers) are passed by value–period. This appears to include primitive values that are properties of objects (or at least closures). The next trick is to find a workaround.

Let me rephrase that. I can think of some ugly workarounds but I’d obviously prefer to find an elegant and efficient one.

This entry was posted in Simulation and tagged , . Bookmark the permalink.

Leave a Reply