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:
Code JavaScript: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.
Code JavaScript:var parent = { numbers: [1, 2, 3], letters: ['a', 'b', 'c'], obj: { prop: 1 }, bool: true };
Code JavaScript://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![]()


Reply With Quote




Bookmarks