0

Hi i have use the following code to create the object

var parent = {};
parent["Task name"] = "Task " + ++x;
parent["Start time"] = "01/03/2013";
parent["End time"] = "01/08/2013";
parent["Duration"] = "5 days";
parent["Status"] = Math.round(Math.random() * 100);

How to clone / take the copy of the object using JavaScript Code . Is there any other way to achieve this?

Raja
  • 209
  • 1
  • 7
  • 16

3 Answers3

1

The simplest way to clone an object is using the following function:

function clone(a){var b=function(){};b.prototype=a;return new b;}

This creates a basic copy of the object, do note though that this does not create a deep copy, only a shallow one.

Entoarox
  • 703
  • 4
  • 8
  • @wared if you get a typeError then the input 'a' is obviously not a valid object, or you are trying to pass in a native object (or no object at all), thus you should expect failure because its designed to handle only javascript objects and nothing else. – Entoarox Jan 05 '14 at 06:29
  • @wared I fixed it (b wasnt being cast as a function), the original code was directly from one of my already minified scripts, something likely went wrong in minification. – Entoarox Jan 07 '14 at 17:35
  • @wared The script is actively being used, so there shouldnt have been any issues, but after checking it turns out the code that uses it hasnt needed the clone method yet, so the error was simply overlooked. – Entoarox Jan 07 '14 at 17:44
  • @wared Of course the clone wont have those properties for itself untill they are changed, its how prototyping works, its why its a `shallow` clone, it mimics the properties of `a` unless those properties on `b` are explicitly set. Despite what you may think, this is the most cost-effective way of cloning an object, and unlike any other method has no risk of recursive loops. That is of course, unless you have the new Object.create available, and even that is far from perfect. Cloning a javascript object is simply incredibly difficult to do, and impossible to do both properly and fast. – Entoarox Jan 07 '14 at 18:04
  • Indeed, you should not use prototyping to clone an object. This has nothing to do with deep or shallow copy, this is called inheritance and it's a completely different topic. –  Jan 07 '14 at 18:11
  • @wared The English word clone has two different possible interpretations in this context, you and Entoarox are using different ones. Whether a `shallow` clone is correct to use depends on use case, and the asker did not specify. – Vitruvie Feb 09 '14 at 20:14
  • @Saposhiente Typo : `console.log(b.hasOwnProperty('x'))`. It would be great if you could elaborate a bit. Afaik, a clone is a copy which has no dependency with the source object, and this is not the case in this situation since `a` is the prototype of `b`. –  Feb 09 '14 at 20:36
  • @wared That definition of clone is what Enthoarox refers to as a `deep` copy, which he specifically says this is not. – Vitruvie Feb 09 '14 at 20:37
  • @Saposhiente I clearly understand what Entarox means by deep copy, this is precisely the point on which we disagree. For me, a deep copy includes nested objects while shallow does not, this has nothing to do with the prototype. Actually, I was waiting for your point of view. –  Feb 09 '14 at 20:43
  • @wared A quick Google search reveals plenty of discussion on "Deep copy vs shallow copy". Please do not use this tone that is (perceived by me as) patronizing; such things are wholly unconstructive. – Vitruvie Feb 09 '14 at 21:14
  • @Saposhiente I've followed your advice, and it appears that the definition of a shallow copy I've found fits with Entarox's one. I've requested a lesson, I got it :/ My apologies. –  Feb 09 '14 at 22:27
0

Try this with jQuery:

var parent = {};
                parent["Task name"] = "Task " + ++x;
                parent["Start time"] = "01/03/2013";
                parent["End time"] = "01/08/2013";
                parent["Duration"] = "5 days";
                parent["Status"] = Math.round(Math.random() * 100);
var newObj = jQuery.extend(true, {}, parent);
Ringo
  • 3,795
  • 3
  • 22
  • 37
0

The most basic way is as follows :

var clone = {};
for (var k in parent) {
    clone[k] = parent[k];
}

Works in this case since all values are of primitive types.