title: What are the possible errors in this Object merging function?
body: <p>I set out to find and understand a nice way to merge objects in Vanilla JS. My requirements for the function are very simple (borrowed from <a href="https://github.com/KyleAMathews/deepmerge" rel="nofollow">here</a>):</p>
  
  <ul>
  <li><p>Merge two objects <code>x</code> and <code>y</code> deeply, returning a new merged object with the elements from both <code>x</code> and <code>y</code>.</p></li>
  <li><p>If an element at the same key is present for both <code>x</code> and <code>y</code>, the value from <code>y</code> will appear in the result.</p></li>
  <li><p>The merge is immutable, so neither <code>x</code> nor <code>y</code> will be modified.</p></li>
  </ul>
  
  <p>I came across <a href="https://gomakethings.com/vanilla-javascript-version-of-jquery-extend/" rel="nofollow">this</a> article that seems to provide a pretty good solution. After going through the code and understanding it (for the most part), I shortened it down to the following: </p>
  
  <pre><code>var extend = function() {
  
    var extended = {};
    var length = arguments.length;
  
    // Merge the object into the extended object
    var merge = function(obj) {
      for (var prop in obj) {
        //Check if a property is an object and another layer of merging is required
        if (Object.prototype.toString.call(obj[prop]) === '[object Object]') {
          extended[prop] = extend(true, extended[prop], obj[prop]);
        } else {
          extended[prop] = obj[prop];
        }
      }
    };
  
    // Loop through each object and conduct a merge
    while (length--) {
      var obj = arguments[length];
      merge(obj);
    }
  
    return extended;
  };
  </code></pre>
  
  <p>From the original solution I removed the check for a deep merge as I would like to deep merge by default, and this line, present before the currently merged property value is checked for being an object: </p>
  
  <pre><code>if ( Object.prototype.hasOwnProperty.call( obj, prop ) )
  </code></pre>
  
  <p>I don't understand this line - why should we check if the object whose properties are currently being looped through has the property from the current loop? I feel like I'm missing something. </p>
  
  <p>So that's it. Are there any cases where this function wouldn't fulfil my requirements? Or break execution on any other way? Thank you. </p>