Infinite loop while trying to find an object

I recently saw this question in which the OP wanted to find the path to an object property, so I answered it in psuedocode, stating that I did not have enough time to actually write a solution. However, the question was so interesting to me that I still tried to write a solution. Here is what I have come up with so far:

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 < 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));

The idea is what I would call this.searchNode()in Node the objects that represented each of the properties of the object. searchNode()will call itself on each of the resulting property nodes, passing the current object as parenton each of the child nodes. If I found a function to search, I would say reconstructPathRecursive()that pretty much does this using the parent property on each of the nodes.

, " ". . , , . , ? console.log , searchNode , , , - ( ...), .

: , isEmpty Node , this.obj searchNode(). ( , ), , . , .

: (. Satyajit). , .

+3
2

var arr = [];
arr[0] = arr;

. Node , .

, , , . JavaScript, .

, , , obj, (breadcrumb), , . , , - , EcmaScript 5.


EDIT:

, .

path = children[i].searchNode();

 path = children[i].searchNode();
 if (path) { return path; } 

EDIT:

, "gotcha" - .

"gotcha"[0] === "g" "g"[0] === "g", , .

alert("gotcha".hasOwnProperty(0));
for (var k in "gotcha") { alert(k); }

Chrome, "true", "0", "1",..., "5".

, 15.5.5.2:

[[GetOwnProperty]], ECMAScript (8.12.1). , String.

- 9 .

+1

nullRoot - , javascript. isEmpty false . , "gotcha" .

+2

All Articles