Objects comparison with recursive function
<p>Consider the following function's blueprint which tries to compare two objects:</p>

<pre><code>function objectCompare(a,b,path){ 
    for (var prop in a) {
      path=prop;
      if (a.hasOwnProperty(prop) &amp;&amp; !(b.hasOwnProperty(prop))) {
                ...
          return false;
      }
      ...
      if (detectType(a[prop])==='Object'){
          if (!objectCompare(a[prop],b[prop],path)) 
               return false;
      }
      ...
    }
      return true;
}
</code></pre>

<p><code>detectType</code> is my own function which checks the type of a variable. My problem is that I want to enrich variable <code>path</code> every time we have a recursive call. However, at the same time when recursive calls finish, <code>path</code> has to traverse the remaining property names of the initial object without being enriched...
Imagine the following objects:</p>

<pre><code>var Obj1 = {
        p1: 's',
        p2: {
          p1: {a: { propA: 'a', propB: 'b' }},
          p2: 'g',
        }
      };

var Obj2 = {
        p1: 's',
        p2: {
          p1: {a: { propA: 'a', propB: 'c' }},
          p2: 'g',
        }
      };
</code></pre>

<p>I want <code>path</code> when function <code>objectCompare</code>returns to have the following value: <code>p2.p1.a.propB</code>  Namely, the point which makes the two objects different. How may I achieve that?</p>