title: Is it possible to deep copy using ES5 JavaScript?
body: <p>Is it possible to deep copy using ES5 JavaScript?</p>
  
  <p>The problem I'm currently having is properties nested more than 2 levels deep appear to be copying by reference instead of by value.</p>
  
  <p>Here's what I've currently implemented to copy ...</p>
  
  <pre><code>// Clone an object and return the clone
  function clone (obj) {
      var newObj = (obj instanceof Array) ? [] : {};
      for (var prop in obj) {
          if(typeof obj[prop] === 'object') {
              newObj[prop] = clone(obj[prop]);
          }
          newObj[prop] = obj[prop]
      }
      return newObj;
  };
  </code></pre>
  
  <p>And here's the code used to test:</p>
  
  <pre><code>const a = { a: 1, b: { c: 2 }};
  const b = clone(a);
  
  a.a = 10;
  b.a == a.a; // false
  
  a.b = 10;
  b.b == 10; // false
  
  a.b.c = 10;
  a.b.c == b.b.c; // true
  </code></pre>
  
  <p>The last one changed the value of <code>c</code> in both <code>a</code> and <code>b</code> objects. The <code>c</code> property appears to be a reference type.</p>
  
  <p>How can I ensure that all values are copied by value and are not references?</p>