Infinite loop when attempting to search an object
<p>I recently saw <a href="http://stackoverflow.com/a/10355185/339852">this question</a> being asked in which the OP wanted to find the path to a property of an object, so I answered it in psuedocode, stating that I didn't have enough time to actually write a solution. However, the question was so interesting to me that I wound up attempting to write a solution anyways. Here's what I've come up with so far:</p>

<pre><code>function isEmpty(obj) {
    for (var prop in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, prop)) {
            return false;
        }
    }
    return true;
}

function Node(obj, parent, searchTarget) {
    this.parent = parent;
    this.obj = obj;
    this.searchTarget = searchTarget;

    this.searchNode = function() {
        if(this.obj == this.searchTarget) {

            //return this.reconstructPathRecursive();
        }

        if (!isEmpty(this.obj)) {
            var children = [];

            for (prop in this.obj) {
                if (this.obj.hasOwnProperty(prop)) {
                    children.push(new Node(this.obj[prop], this, searchTarget));
                }
            }

            var path;
            for(var i = 0, len = children.length; i &lt; len; i++) {
                path = children[i].searchNode();
                if(path) return path;
            }
        }
    }

    this.reconstructPathRecursive = function() {
        var path = [this], curObj = this.parent;

        while (curObj != undefined) {
            path.push(curObj);
            curObj = curObj.parent;
            if(curObj == undefined) break;
        }

        return path;
    }

    this.findPath = function() {
        return this.searchNode();
    }
}

var myObj = {
    nullRoot: "gotcha!",
    path1: {
        myFunc: function() {
            alert("Success!");
        }
    }
}

function findFunctionPath(obj, func) {
    return new Node(obj, undefined, func).findPath();
}
var thisFunc = myObj.path1.myFunc;
    console.log("--");

console.log(findFunctionPath(myObj, thisFunc));
</code></pre>

<p>The idea is that I would call <code>this.searchNode()</code> on Node objects which represented each of the object's properties. <code>searchNode()</code> would call itself on each of the resulting property nodes, passing in the current object as the <code>parent</code> on each of the children nodes. If I found the function to search for, I would call <code>reconstructPathRecursive()</code>, which pretty much does that, using the parent property on each of the nodes.</p>

<p>However, I get a 'Maximum call stack size exceeded.' error when I run this <a href="http://jsfiddle.net/HeyJavascript/yc2VN/10/" rel="nofollow">live test</a>. I assume it means I accidentally wrote an infinite loop somehow. Where's the flaw in my logic, and where did that infinite loop sneak in? <code>console.log</code> shows that <code>searchNode</code> is being called over and over again, whereas I'm only calling it if the object isn't empty, and I'm not giving the object a reference to itself anywhere (I don't think...), so I'm really kind of stumped here.</p>

<p><strong>Edit:</strong> I updated the code somewhat to change <code>isEmpty</code> from a Node function to a global function, so that I could call it on <code>this.obj</code> in the <code>searchNode()</code> function. Previously, it would only call it on Nodes (which will always have at least two properties, thus resulting in an infinite loop), not the objects the referenced. That's fixed, but the error persists.</p>

<p><strong>Another edit</strong>: Found and corrected another error (see Satyajit's answer). Still getting the infinite loop, though.</p>
