The below first logs 0, and then logs 1. How do I store a copy of the object, rather than a reference to it?
debug.log(vi.details.segment);
vi.nextSegment = vi.details;
vi.nextSegment.segment++;
debug.log(vi.details.segment);
The below first logs 0, and then logs 1. How do I store a copy of the object, rather than a reference to it?
|
|||
|
To clone an object in jQuery:
NOTE: The above is a shallow copy: any nested objects or arrays will be copied by reference - meaning any changes you make to
To read up more on extend, see here. |
|||||||||||||||||
|
Take a look at the post: What is the most efficient way to clone a javascript object As per John Resig's answer:
More information can be found in the jQuery documentation. |
|||
|
This worked better for me cloning an object using jQuery "parseJSON()" and "JSON.stringify()"
Cloning data object in objX & objY are two different object, you do no have to mess up with the "by reference" problem Gracias! |
|||||
|
Another way to clone object is
But be careful if it's contains dates. JSON.parse will return date.toString() instead of date in that case. |
|||
|
This is how I copy elements several times: First I have a template:
Now, the JavaScript:
|
|||
|