Art Of Programming

musings by Dmytrii Nagirniak

Search for a Property Somewhere in JavaScript

I know the part of the property name in some JS object, but have no idea in what object this property is located.

So I wrote ridiculous script to find it for me. Just a lazzy developer. Please don’t use anything like that in production.

var findPropertyCompare=function(srch, existing) {
 return existing.toString().indexOf(srch) >= 0;
}

var findProperty=function(n, root, path, depth) {
  depth = depth || 0;
  if (depth <= 5) {
    for(var key in root) {
      if (findPropertyCompare(n, key)) {
        console.info("Found %s = %o", path + '.' + key, root[key]);
      }
      findProperty(n, root[key], path + '.' + key, depth+1);
    }
  }
};

findProperty('FCK', window, 'window');

 

It just outputs all the properties that contain ‘FCK’. Of course it only works in FF because of console object is used. Also it doesn’t handle exceptions that may occur (and will occur).

But anyway, still useful.

Comments