0

jQuery has made my life easier but I'm still very beginner to JavaScript. So, may be, I'm asking a very stupid question here:

var t = {
    rows: 3,
    columns: 5,
    getCellCount: function () {
        return this.rows * this.columns;
    }
};
var tn = t;
tn.rows = 6;
document.write(tn.rows + " , " + t.rows);   // returns 6 , 6

I also tried var tn = new t(); // but seems wrong

So, How to retrieve old intrinsic value from object so that it results 6 , 3

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • `var tn = t;` doesn't clone the Object itself, it just copies the reference to that Object. "[Most efficient way to clone an object?](http://stackoverflow.com/questions/122102/most-efficient-way-to-clone-an-object)" may help with the other. – Jonathan Lonowski Jan 20 '14 at 03:42
  • So, how can I clone so the result may 6,3 – Bhojendra Rauniyar Jan 20 '14 at 03:43
  • 1
    See the aforementioned question. But, one option is [`var tn = Object.create(t);`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) so `tn` inherits from `t` rather than them being the same. – Jonathan Lonowski Jan 20 '14 at 03:44

5 Answers5

5

tn and t are both pointing to the same object, that's why when you change tn.rows it also changes t.rows. There is no such thing as old intrinsic value.

You must copy the object in order to keep the old value. There are two: shallow copy and deep copy.

Copying the key-value pairs is pretty easy with Object.create.

var newObj = Object.create(oldObj);

Now if you change the values in newObj, it will not change the original one:

var a = {a:1}, b = Object.create(a);
b.a = 2;
console.log(a.a, b.a);  //1,2

However, to perform a complete copy is really complicated. See more: How do I correctly clone a JavaScript object?

PS: The new keyword you mentioned is for creating an object as in classes.

function Car(n, y){                              //This is called a "constructor"
    var name = n, year = y;                      //Private variables
    return {
        getName: function(){ return name; },     //Getters
        getYear: function(){ return year; }
    };
}
var myNewCar = new Car("Toyota", 2010);
myNewCar.getName();  //"Toyota"
myNewCar.getYear();  //2010

(This is also how you create objects in Java, if you have taken those CS courses you would recognize this pattern.)

Community
  • 1
  • 1
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
1
var tn = t;

simply makes both tn and t to point to the same object in memory. So, change in one object will reflect in other as well. You can clone the object, like this

function copyObject(sourceObject) {
    var result = {};
    for (var key in sourceObject) {
        if (sourceObject.hasOwnProperty(key)) {
            result[key] = sourceObject[key];
        }
    }
    return result;
}
var tn = copyObject(t);
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

You are asking to clone a JSON object.

var tn={}; 
for (key in t) tn[key]=t[key];

There may be other "prettier" ways, but this guarantees the clone.

Schien
  • 3,855
  • 1
  • 16
  • 29
  • This will only perform a shallow copy. What if a deep copy is needed? For instance, `getCellCount` will refer to the same function object in memory if copied like this. That may not have meaningful consequences for this particular case, but it does if one of the properties is another JS object. – ajp15243 Jan 20 '14 at 03:54
  • You are right. Context does matter. I think the question has taken a life of its own and evolved to "how to copy a generic JSON object". My solution doesn't work with the generic case where, as you pointed out, a deep copy is required. – Schien Jan 20 '14 at 03:58
0

The = operator in javascript just changes what the object points to, so it will not create a copy of the original object. You can take a look here to see possible ways to create a clone of the object.

Community
  • 1
  • 1
SuperTron
  • 4,203
  • 6
  • 35
  • 62
0

If you want to create a Javascript object, the conventional way is to create a function:

// Use capitalized names for Object-creating functions.
// I guessed that you wanted a matrix of some sort.
function Matrix(rows, columns) {
    this.rows = rows;
    this.columns = columns;
    this.cellCount = function() {
        return this.rows * this.columns;
    }
    this.dimensions = function() {
        return this.rows, + ", " + this.columns;
    }
    this.copy = function() {
        return new Matrix(this.rows, this.columns);
    }
}

var t = new Matrix(6, 3);

There are more sophisticated ways to do this, using Object.create. Look at Javascript: The Good Parts.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29