SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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:

    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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by shurns View Post
    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 || { };
    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.

    Quote Originally Posted by shurns View Post
    for(var i in p) {
    //What's the meaning of using the constructor property to check the object?
    if(typeof p[x].constructor ==== 'object') {
    The meaning is to check if p[x] is something that has a deeper structure that needs to be copied

    Quote Originally Posted by shurns View Post
    //What's the meaning of the ternary operator having the { } object?
    c[x] = (p[x] === Array) ? [] : { };
    It sets up the c[x] object that will be used to store the content of the next deeper level of content.

    Quote Originally Posted by shurns View Post
    //I don't get what's the meaning of invoking deepCopy().
    deepCopy(p[x], c[x])
    The purpose of it is to go down another level and copy what's in there.

    Quote Originally Posted by shurns View Post
    Code JavaScript:
    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.
     
    //I thought you're required to use two arguments in the deepCopy method.
    var mydeep = deepCopy(parent);
    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.

    Quote Originally Posted by shurns View Post
    This type of code looks advance and very hard to analyze.
    Is this code important to learn?
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your answers much appreciated

    //What's the meaning of the ternary operator having the { } object?
    c[x] = (p[x] === 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.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by shurns View Post
    Thanks for your answers much appreciated

    //What's the meaning of the ternary operator having the { } object?
    c[x] = (p[x] === Array) ? [] : { };
    Perhaps an expanded view of that code will help to aid in some understanding.

    Code javascript:
    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.

    Quote Originally Posted by shurns View Post
    One quick question regarding the array literal in the ternary operator, does it creates a new array?
    Yes it does.

    Quote Originally Posted by shurns View Post
    I've been learing about javascript for about a year it's just this type of coding is way out of my league.
    Is this info about the ternary operator of any help then?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by shurns View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •