3

I'd hope to find an example code to do a deep copying of objects in ECMAScript5.

The copying should be able to clone

  • Nested objects

  • Nested arrays

  • Nested objects in arrays (clone each array item individually)

Note: jQuery.extend() does not seem to handle case 3). Also, I'd hope to do this in clean ECMAScript. Quick googling did not bring up any worthy implementations.

2
  • Deep copying is a non trivial problem, I recommend you change your algorithm so it works with a shallow copy Commented Nov 29, 2011 at 2:47
  • Currently I am doing it with jQuery.extend() and manually cleaning up the mess afterwards Commented Nov 29, 2011 at 12:09

3 Answers 3

1

I finally settled to jQuery.extend() as I couldn't find other good implementations

http://api.jquery.com/jQuery.extend/

Sign up to request clarification or add additional context in comments.

Comments

1

if you want a one-liner (removes object refs by iterating through referenced objects to retrieve primitives, concats one large string, then parses the string into a new object with it's own primitive leaf nodes)

JSON.parse(JSON.stringify(obj))

or if you need to perform many copies

function deepCopy(o) {
    var copy = o,k;

    if (o && typeof o === 'object') {
        copy = Object.prototype.toString.call(o) === '[object Array]' ? [] : {};
        for (k in o) {
            copy[k] = deepCopy(o[k]);
        }
    }

    return copy;
}

performance comparison

2 Comments

What do you mean? This does perform a deep copy. Its not specifically ECMAScript5 but neither is jQuery right?
Sorry! What I meant to say is please explain what's the code doing?
0

Use an emulation of the toSource method to copy the object:

    <script type="text/javascript">
    Object.prototype.getSource = function() {
      var output = [], temp;
      for (var i in this) {
          if (this.hasOwnProperty(i)) {
              temp = i + ":";
              switch (typeof this[i]) {
                  case "object" :
                      temp += this[i].getSource();
                      break;
                  case "string" :
                      temp += "\"" + this[i] + "\"";    // add in some code to escape quotes
                      break;
                  default :
                      temp += this[i];
              }
              output.push(temp);
          }
      }
      return "{" + output.join() + "}";
      }
      var baz = {"alpha":{"beta":{"charlie": ["delta","epsilon",{"omega":"zeta"}]}}};
      !!Object.prototype.toSource ? alert((baz).toSource() ) : alert((baz).getSource() );
    </script>

2 Comments

A start, but isn't this little fragile.. possible infinite loop with cyclic references?
Contact the developer of this solution for help with robustness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.