-2

i want to assign one object to an other object to change one object and still have the original object remain unchanged. Since the two objects become just one in the storage with two pointers pointing at it this does not work. How do copie objects with different pointers but the same values.
What is the propper way to do that?

$(document).ready(function(){
    
    var object1 = new theObject("hello");
        
    var object2 = new theObject("hello again");
        
    object1 = object2;
    
    object1.value = "newValue";
    
    //  object1.value = "new Value"
    //  object2.value = "new Value"  //should be "hello again"
    
  alert("object1: " + object1.value + " object2: " + object2.value );
  
});

class theObject{
    constructor(value){
        this.value = value;
    }
}

this works but its not so nice if you have a return value that is an object:

var objKeys = Object.keys(object1);
for (let objKey of objKeys){
    object1[objKey] = object2[objKey];
}
Nils
  • 275
  • 3
  • 12
  • I'm not sure I understand; you have two objects here, but you promptly throw one away. If you want to set the *value* of an object then set the value of that object, don't reassign it to reference a different object. If this is just a bad example then you'd need to provide a mechanism to copy one object's values into another. – Dave Newton Aug 17 '20 at 20:37
  • you can't reassign like that expecting to update a reference or create a new one. they point to the same object. – Daniel A. White Aug 17 '20 at 20:38
  • consider using something like immutable. – Daniel A. White Aug 17 '20 at 20:38
  • lodash has a method "cloneDeep" that will make a fairly good attempt at making a copy of an object with all new references. – Charlie Bamford Aug 17 '20 at 20:42

2 Answers2

1

Probably the most bulletproof way to deep clone an object if it just holds strings/numbers is the following:

var object2 = JSON.parse(JSON.stringify(object1));

Keep in mind if it contains values other than strings/numbers, those will be stringified.

inorganik
  • 24,255
  • 17
  • 90
  • 114
0

Instead of assigning the reference of object2 to object1, you can clone it. For example :

class theObject {
  constructor(value) {
    this.value = value;
  }
  clone() {
    return new theObject(this.value);
  }
}

var object1 = new theObject("hello");
var object2 = new theObject("hello again");

object1 = object2.clone(); 
object1.value = "newValue";

console.log("object1: " + object1.value);
console.log("object2: " + object2.value);
Slai
  • 22,144
  • 5
  • 45
  • 53
  • what i wanted is send an objectOne to a function, change it there, return it and save it in an ObjectTwo without changing the ObjectOne. – Nils Aug 17 '20 at 20:53
  • 1
    @nils Clone ObjectOne before you send it to the function, then send your shiny new ObjectTwo. Then you don't have to mutate the object you don't want to change. – Charlie Bamford Aug 17 '20 at 21:09
  • @Nils modify cloned objectOne instead of the objectOne. Either pass cloned objectOne to the function, or clone it inside the function and modify the cloned object before returning it. – Slai Aug 17 '20 at 21:43