Confused about the deep copy function (inheritance)

I’ve learned that using the deep copy in inheritance fixes the problem when you’re inheriting a child object when later modifying a child’s property, you’re changing the parent’s property as well because it points to the same object.

I read this awesome book but I’m very confused about what this code means:

function deepCopy(p, c) {
     //Couldn't you just put c = { } instead?
     var c = c || { };

     for(var i in p) {
          //What's the meaning of using the constructor property to check the object?
          if(typeof p[x].constructor ==== 'object') {
               //What's the meaning of the ternary operator having the { } object?
               c[x] = (p[x] === Array) ? [] : { };
               //I don't get what's the meaning of invoking deepCopy().
               deepCopy(p[x], c[x])
          }
          else {
               c[x] = p[x];
          }
     }
     return c;
}

Sorry if I confuse anyone in my code comments. It would be appreciated if anyone can just walk me through the use of the meaning of the code inside the for-in loop.

var parent = {
	numbers: [1, 2, 3],
	letters: ['a', 'b', 'c'],
	obj: {
	     prop: 1
	},
	bool: true
};
//I thought you're required to use two arguments in the deepCopy method.
var mydeep = deepCopy(parent);

This type of code looks advance and very hard to analyze.
Is this code important to learn?

Thanks in advance :slight_smile:

No you couldn’t, for that would end up clobbering the second parameter, which in nested copies is the object that you are copying to.

The meaning is to check if p is something that has a deeper structure that needs to be copied

It sets up the c object that will be used to store the content of the next deeper level of content.

The purpose of it is to go down another level and copy what’s in there.

The arguments of a function don’t need to be provided. When they’re not, the function just ends up with an undefined variable in its place instead.

It can be useful to learn more about what is occurring and why, if only so that you can have some idea of what’s possible.

Thanks for your answers much appreciated :slight_smile:

//What’s the meaning of the ternary operator having the { } object?
c = (p === Array) ? : { };

One quick question regarding the array literal in the ternary operator, does it creates a new array?

I’ve been learing about javascript for about a year it’s just this type of coding is way out of my league.

Perhaps an expanded view of that code will help to aid in some understanding.


if (p[x] === Array) {
    c[x] = [];
} else {
    c[x] = { };

So what that is doing is to set up an appropriate container (array or object) to contain what will be copied in to it.

Yes it does.

Is this info about the ternary operator of any help then?

I understand how the ternary operator works. I was just a bit confused regarding literal whether it was creating a new array or modifying it within the child object. I understand this part now.

That’s good to know.

Values such as strings or numbers or booleans, those can be copied straight across without any trouble.
The only two situations where a deeper copy is needed is in regards to nested information, such as arrays or objects. That’s why that’s being done.

1 Like