0

This is what I want to do:

I have an object that stores a state and also an array of the previous states.

var object = { currentState: [Object], previousStates: [[Object], [Object]] };

When the state changes I want to store the reference to currrentState's object in the previous states array so that it can be reused, then create a new state (or reuse an old state) and store it in currentState WITHOUT changing the original object passed to the array. If it makes any difference at all, the states are instantiated class objects.

Is this possible?

I have looked at lots of resources on passing by reference and passing by value but I think I need it explained in the context of an example. Which example will store a new state at currentState without changing the previous value pushed to the array if either?

function changeState1(states) {
    states.previousStates.push(states.currentState);
    states.currentState = new State();
}

function changeState2(states) {
    var newState = new State();
    states.previousStates.push(states.currentState);
    states.currentState = newState;
}

EDIT:

I should probably mention that my intention is to create efficient code. I don't want to recreate states from classes on the fly if I can reuse old instances. It's better for memory usage and reduces garbage collection.

smmaca
  • 85
  • 1
  • 5
  • 2
    *"resources on passing by reference and passing by value"* JavaScript is *pass-by-value*. The fact that objects are represented **as** references has nothing to do with pass **by** reference. *Pass by value/reference* describes the relationship between *bindings* (i.e. variables and parameters) not *value*. See https://en.wikipedia.org/wiki/Evaluation_strategy for more information. – Felix Kling Mar 30 '17 at 23:37
  • @FelixKling Yes, I have seen this mentioned multiple times everywhere but I'm not interested in semantics. I just want to know if what I want to code is possible in JavaScript – smmaca Mar 30 '17 at 23:39
  • 3
    I'm not really sure I understand what exactly you are trying to achieve. A concrete example would help. *"but I'm not interested in semantics"* well, knowing the right semantics will help you not wasting your time searching for unrelated information, or misdirecting those who want to help you. – Felix Kling Mar 30 '17 at 23:44
  • @FelixKling I've added an example of what I might do to solve the problem. You're right about semantics. I'm just finding it all very confusing. – smmaca Mar 31 '17 at 00:00
  • I agree that it can be confusing, which is why I think it's even more important to get the semantics right :) Your example will work just as you want it to work. – Felix Kling Mar 31 '17 at 00:03
  • @FelixKling Which function is correct? Or are they essentially the same? – smmaca Mar 31 '17 at 00:08
  • They are the same. – Felix Kling Mar 31 '17 at 00:18

1 Answers1

2

Maybe something like this?

var obj = newObjectState; object.previousStates.push(object.currentState); object.currentState = obj;

Of course this is all a kind of pseudocode but could be the answer you are looking for?

Lixus
  • 511
  • 2
  • 12
  • This was my instinctual answer but I have noticed that some similar things have had unintended effects. E.g. I created an object then assigned two variables to be that object then changed one of the properties on one of the variables. The other variable's property changed too. – smmaca Mar 30 '17 at 23:42
  • @smmaca: Yes, since objects are represented as references, both variables will point to the same object. But if the variables refer to two different objects, that wouldn't happen. – Felix Kling Mar 30 '17 at 23:47
  • @smmaca: To add to Felix's comment, yes, modifying the variable's property will modify the referenced object, as you've noticed. But setting the variable to something else has no effect on the object at all and simply cancels the reference. – Dan Mar 30 '17 at 23:49
  • That's because the way javascript saves references, in your example both variables reference the same object, they are not copies of your original one. This answer explains it better than me do http://stackoverflow.com/questions/29050004/modifying-a-copy-of-a-javascript-object-is-causing-the-original-object-to-change You have to make copies of your object and not assign them to other variables. – Lixus Mar 30 '17 at 23:49
  • @LuisFernandoRodríguez So, if I understand correctly, I have to create a new variable with my new state then assign that to currentState instead of just the new state? Then current state references a new value instead of having it's current reference's value changed? – smmaca Mar 30 '17 at 23:58
  • 1
    @smmaca: I'll answer this for Luis. No, that's not necessary. Both of your `changeState` functions will work. As long as the object is still referenced by some variable, internal garbage collection will not get rid of it. – Dan Mar 31 '17 at 00:19