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

3
  • var tn = t; doesn't clone the Object itself, it just copies the reference to that Object. "Most efficient way to clone an object?" may help with the other. Commented Jan 20, 2014 at 3:42
  • So, how can I clone so the result may 6,3 Commented Jan 20, 2014 at 3:43
  • 1
    See the aforementioned question. But, one option is var tn = Object.create(t); so tn inherits from t rather than them being the same. Commented Jan 20, 2014 at 3:44

5 Answers 5

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

I'd upvote, but this doesn't explain how to achieve the separated objects
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);

1 Comment

@C-link We can write a helper function like I have shown in the answer.
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.

2 Comments

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.
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.
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.