HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

JavaScript function for to deep copy objects

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
objectsjavascriptfunctiondeepforcopy

Problem

I've made this function for to get a deep copy (recursive copy) of objects.
=> Sub-objects aren't references to the sub-objects of the original object. Instead they are objects on their own.

Here's my code ...

Attention: Most of it are test data and an additional test-function.



`// ----- Test data ---------------------
var swapi = [ {
"name": "Luke Skywalker",
"height": "1.72 m",
"mass": "77 Kg",
"hair_color": "Blond",
"skin_color": "Caucasian",
"eye_color": "Blue",
"birth_year": "19 BBY",
"gender": "Male",
"homeworld": "http://swapi.co/api/planets/1/",
"films": [
"http://swapi.co/api/films/1/",
"http://swapi.co/api/films/2/",
"http://swapi.co/api/films/3/"
],
"species": [
"http://swapi.co/api/species/1/"
],
"vehicles": [
"http://swapi.co/api/vehicles/14/",
"http://swapi.co/api/vehicles/30/"
],
"starships": [
"http://swapi.co/api/starships/12/",
"http://swapi.co/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-10T13:52:43.172000Z",
"url": "http://swapi.co/api/people/1/"
},
{
"name": "C-3PO",
"height": "167",
"mass": "75",
"hair_color": "n/a",
"skin_color": "gold",
"eye_color": "yellow",
"birth_year": "112BBY",
"gender": "n/a",
"homeworld": "http://swapi.co/api/planets/1/",
"films": [
"http://swapi.co/api/films/5/",
"http://swapi.co/api/films/4/",
"http://swapi.co/api/films/6/",
"http://swapi.co/api/films/3/",
"http://swapi.co/api/films/2/",
"http://swapi.co/api/films/1/"
],
"species": [
"http://swapi.co/api/species/2/"
],
"vehicles": [],
"starships": [],
"created": "2014-12-10T15:10:51.357000Z",
"edited": "2014-12-20T21:17:50.309000Z",
"url": "http://swapi.co/a

Solution

Here some points I got from your code.

The use of var and let.

The var keyword defines a variable with a scope of the function where the variable is declared.

The let keyword defines a variable with local scope.

In your case the ret variable could be also declared using the let keyword, and it works because it is declared at the top of the function, so the local scope is the whole function.

But, in this case you're right to use var, just to indicate that the variable have a function scope.

However, you should use let in the for loop:

for (var key in obj) {
    ...


Here the variable key should not have a function scope at all.

The second point is still in your for loop.

You check the key if exists inside the object obj, but you don't need this check, as you are using the in operator that gets an object's real key.

So just remove this part.

I don't know where you use this code, so I'm not sure about the usefulness of this suggestion, but you could add a parameter to limit how deep you're copying.

The reason is to avoid issues in case of very deep object structures, and circular references.

You could check if the parameter is set and limit the copy just in that case.

About improving your test: to be sure that copy is a different object to original, and not just a reference, you should add some negative checks, like changing the original object and test that the value in the copy object is not changed. Or add a new property, and check that it is not in the copy.

Code Snippets

for (var key in obj) {
    ...

Context

StackExchange Code Review Q#141511, answer score: 4

Revisions (0)

No revisions yet.