0

I've got 2 different objects: pos_cashinout and pos_cashinout_lokaal.

First I set pos_cashinout equal to pos_cashinout_lokaal. Then I set one key of pos_cashinout_lokaal to null, but when I check the values they become null for both objects.

This is my code:

pos_cashinout = pos_cashinout_lokaal;
window.alert(pos_cashinout.toSource());
window.alert(pos_cashinout_lokaal.toSource());
             
pos_cashinout_lokaal.verrichting = null;
             
window.alert(pos_cashinout.verrichting);
window.alert(pos_cashinout_lokaal.verrichting);

The output of the window alerts in the same sequence:

({aan:"Jos", bedraguit:10, kassier:"Robbe", opmerking:"Einde dag", bedragin:10013, id:1, verrichting:"Your Company"})

({aan:"Jos", bedraguit:10, kassier:"Robbe", opmerking:"Einde dag", bedragin:10013, id:1, verrichting:"Your Company"})

null

null

I thought, and intended that only pos_cashinout_lokaal.verrichting would be null?

edit: objects are indeed only referenced the way I did it. Found the solution here: How do I correctly clone a JavaScript object?

I got it working like this:

cloneobject: function(obj){
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
},

pos_cashinout = this.cloneobject(pos_cashinout_lokaal)
Community
  • 1
  • 1
RobbeM
  • 727
  • 7
  • 16
  • 36

0 Answers0