The short version:

  • How can I get a list of all the objects (including their descendant objects) on the page (not just the first-depth objects)?
    • Anticipated subproblem: How can I track the visited objects as I walk the objects?

Thanks in advance.



The long version (with background!!):

Using the in keyword we can get all the properties of an object. (And using the hasOwnProperty method allows us to filter out only the properties that belong to that object and not the inherited ones.)

for (var prop in obj) {
   if (typeof(obj[prop]) == 'object'  && obj.hasOwnProperty(prop)) {
            showObjectsOfInternal(obj[prop], visitedObjects); // recursion.
   }
}

That is a good starting point, but I would like to get all the objects. One could imagine iterating through all the properties and accumulating the objects, then recursively iterating through those. However, if there was an object reference loop, like an object referred to itself, such as in window.window, it would be good not to get trapped by that. So one needs a way to track all the 'visited objects' during the recursion.

To track the visited object, one really needs a hashset of objects, based on their internal object key. I tried that by making a visitedObjects object and setting it's keys as the object to be added, and the value didn't matter.

if(visitedObjects[obj] == 1){return;}
visitedObjects[obj] = 1;

But that didn't work for me. (It seems to turn the objects into strings for keys, instead of using their internal reference keys)

So instead I decided to use an array and add an indexOf method.

Array.prototype.indexOf = function(obj){
   for(var i = 0; i < this.length; i++)
   {
      if(thi
Edit
Report