-2

So i have an javascript object called gamewhich has two methods:

function game() {
    this.TxtRevealed = new Array();
    this.TxtRevealedBackup = new Array();
[...]
}

Now, outside an object i assign one two another:

game.TxtRevealedBackup = game.TxtRevealed;

After a while i change game.TxtRevealed(i use slice function to cut some values from it). And now happens something i do not intend: automatically game.TxtRevealedBackup changes also to new value of game.TxtRevealed.

I'd expect that game.TxtRevealedBackup would be same as game.TxtRevealed was in moment of assigning. It works as if game.TxtRevealedBackup was pointing to value that is represented by game.TxtRevealed continously, not the value it was in moment of assignment.

Why is it happening and how to make it working i'd expect?

Kalreg.

Kalreg
  • 982
  • 1
  • 13
  • 27

1 Answers1

0

Objects do work by reference. If you want to clone objects see this answer.

Community
  • 1
  • 1
reieRMeister
  • 121
  • 2
  • 9
  • i'd expect that there is much more simple solution than that, something like adding & to name of variable or sth. – Kalreg Mar 23 '16 at 21:13
  • Nope. But if you are using jQuery you could use something like `var cloned = $.extend({}, original);`. – reieRMeister Mar 23 '16 at 21:38