5

this is my first time here.

So the problem is, i have an object with all my variables like this:

app.Variables = {
    var1: 0,
    var2: 0,
    var3: 0
}

And i want to store this values in a object called Defaults like this:

app.Defaults = app.Variables

But the problem now is, in my code, app.Variables.var1 e.g. get incremented like this:

app.Variables.var1++

And this means, that app.Defaults.var1 get also incremented equal to app.Variables.var1.

What shall i do here?

DerToti
  • 53
  • 3

2 Answers2

2

Simplest version is to use JSON.parse/stringify, fastest is to use a plain clone method:

/* simplest */
var clone = JSON.parse(JSON.stringify(obj));

/* fastest */
function clone(obj) {
    if (obj == null ||typeof obj != "object") return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}
var clone2 = clone(obj);
flavian
  • 28,161
  • 11
  • 65
  • 105
Christoph
  • 50,121
  • 21
  • 99
  • 128
  • thx for all anwers :) i used the simplest version and it works :) later in my code, the object get cloned back the same way – DerToti Jul 09 '13 at 10:59
1

You could write a deep clone Method, which copies every value of every property of your Object to a new one.

Note i extend Object.prototype to avoid type checking and for simplicities sake, this could be changed, if you feel unpleasent with it

Object.defineProperty(Object.prototype, "clone", {
    enumerable : false,
    value: function(deep) {
    deep |= 0;      
    var type = typeof this;
    if (type !== "object") {
        return this.valueOf();
    }

    var clone = {};
    if (0 === deep) {
        for (var prop in this) {
            clone[prop] = this[prop];
        }
    } else {
        for (var prop in this) {
            if ( typeof this[prop] !== "undefined" && this[prop] !== null)
                clone[prop] = ( typeof this[prop] !== "object" ? this[prop] : this[prop].clone(deep - 1));
            else
                clone[prop] = "";
        }
    }
    return clone;
  }
});

Object.defineProperty(Array.prototype, "clone", {
    enumerable : false,
    value:function(deep) {
    deep |= 0;
    var clone = [];
        if (0 === deep)
            clone = this.concat();
        else
            this.forEach(function(e) {
                if ( typeof e !== "undefined" && e !== null)
                    clone.push(( typeof e !== "object" ? e : e.clone(deep - 1)));
                else
                    clone.push("");
            });
    return clone;
  }
});

Example output and a Demo

var first = {
  var1:0,
  var2:0
  var3:0
};
var second = first.clone(Infinity);
first.var1++;
console.log (first.var1,second.var1,second); //1 , 0

To apply this to your code, you just have to clone the Object app.Defaults = app.Variables.clone()

The first argument is the level of deepness. If omitted, only the first level is cloned, which would be enough in this case.

Moritz Roessler
  • 8,542
  • 26
  • 51