15

On page load I am creating two Javascript Objects, objDemo1 and objDemo1Backup where the latter is simply an exact copy of the first.

e.g.

objDemo1 { 
    sub_1 = { something: 123, somethingElse: 321 },
    sub_2 = { something: 456, somethingElse: 654 }
}

I can modify the values in sub_ as well as add / delete new sub_'s but the only object I am editing is objDemo1. i.e. I never change objDemo1Backup

I have a reset button that when clicked will reset objDemo1 back to what it was when the page originally loaded (i.e. objDemo1 = objDemo1Backup). This is where I am having the issue..

How do I set objDemo1 to objDemo1Backup?

I have tried:

objDemo1 = objDemo1Backup;

and

objDemo1 = null;
var objDemo1 = objDemo1Backup;

...as well as similar variations but nothing seems to work. Any ideas?

  • Note: I can confirm that at the point of resetting, objDemo1Backup is exactly the same as it was when I created it and objDemo1 has changed.
  • My code is definetly hitting the "reset" functionality, where I've tried the objDemo1 = objDemo1Backup... I just cannot figure out the syntax to replace the object.
2
  • 1
    I'm going to assume that what is happening is that since both objDemo1 and objDemo1Backup point to the same object, when you change one, both are changed. This may not seem intuitive, but it is the way javascript works. Google "javascript clone object" for more details. Commented Dec 18, 2012 at 16:46
  • 1
    You're playing with object reference, not cloning it. You should clone your object instead of assign it using = Commented Dec 18, 2012 at 16:47

3 Answers 3

11

I'm using angularjs and it took me some time to find out how to copy an object to another object. Normally you'll get an objects clone by calling clone or here in angular copy:

var targetObj = angular.copy(sourceObj);

This gives you a new cloned instance (with a new reference) of the source object. But a quick look into the docs reveals the second parameter of copy:

angular.copy(sourceObj, targetObj)

This way you can override a target object with the fields and methods of the source and also keep the target objects reference.

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

2 Comments

Exactly what I was looking for. I didn't know angular.copy could take a target object. Thanks!
There is no AngularJS tag in this question.
7

In JavaScript objects are passed by reference, never by value. So:

var objDemo, objDemoBackup;
objDemo = {
    sub_1: "foo";
};
objDemoBackup = objDemo;
objDemo.sub_2 = "bar";
console.log(objDemoBackup.sub_2);   // "bar"

To get a copy, you must use a copy function. JavaScript doesn't have one natively but here is a clone implementation: How do I correctly clone a JavaScript object?

var objDemo, objDemoBackup;
objDemo = {
    sub_1: "foo";
};
objDemoBackup = clone(objDemo);
objDemo.sub_2 = "bar";
console.log(objDemoBackup.sub_2);   // undefined

2 Comments

So if I wanted to reset objDemo back to what objDemoBackup was a point of cloning I would do this: objDemo = clone(objDemoBackup);
Yes, you must clone it again or you'll end with a reference :)
-2

You can use Object.assign.

ObjectConstructor.assign(target: T, source: U): T & U

It takes up two parameters: target and source. When function completes, all internals of target object will be appended with source one.

3 Comments

Not exactly, it is a merge not replacement. In the MDN example in your link, the updated target has property "a" and the source does not. In the OP's example, the properties have the same names so this is not an issue.
As @AlienTechnology stated Object.assign doest not create a new instance
Object.assign does not replace the object, it merges with it.

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.