skip to Main Content

I’m importing a library in a project and I can’t find a method that I’m supposed to use.

The functions name is itDoesntMatterFunctionName().

Is there a search by function name in the chrome debugger tools that if I give it an object it will find it?

Example:

var myApp = new ComplexApplication();
console.find(myApp, "myFunctionName");

It’s entirely possible that this function is not exported by the module because note this error:

Uncaught SyntaxError: The requested module './lib/pixi/pixi.mjs' does not provide an export named 'getCanvasBoundingBox'

But it does find other classes / objects in the module.

Chrome debugger / Dev tools Filter box (does not find methods or properties on objects which is what I want):
enter image description here

The Javascript pseudocode for this would be something like:

var object = window.document;
var searchString = "body";
// loop through all objects and property names on the object
while (object typeof object) {
    if (object[searchString]!==undefined) {
        // return object path
    }
    // loop through all properties on object recursively
}

2

Answers


  1. in my personal libraries i call this an apiExplorer and it goes something like this, its fairly primitive and could do with going to version 2, any help would be great, for instance it doesnt check for circular references

    function apiexplorer(mod, maxdepth, name) {
    
      maxdepth = maxdepth || 2;
      var list = [];
    
      var o = gen(mod, '', 0);
      if (o) {
        return o;
      }
      return list;
    
      function gen(mod, pre, depth) {
    
        if (depth === maxdepth) {
          return;
        }
    
        for (var key in mod) {
    
          var o = mod[key];
    
          if (name === key) {
            return o;
          }
    
          var type = typename(o);
    
          if (pre) {
            key = pre + '.' + key;
          }
    
          if (key === name) {
            return o;
          }
    
          add(key, type);
    
          if (!isprimitive(o)) {
            var o = gen(o, key, depth + 1);
            if (o) {
              return o;
            }
          }
    
        } //for
    
      } //gen
    
    
      function add(key, type) {
    
        var o1 = {
          key,
          type
        };
        var n = list.length;
        for (var i = 0; i < n; i++) {
    
          var o = list[i];
          if (o.key > key) {
            list.splice(i, 0, o1);
            return;
          }
    
        } //for
        list.push(o1);
    
      } //add
    
    
      function isprimitive(v) {
    
        switch (typeof v) {
    
          case 'null':
          case 'undefined':
          case 'number':
          case 'string':
          case 'boolean':
            return true;
    
        } //switch
    
        return false;
    
      } //isprimitive
    
    
      function typename(v) {
    
        var str = Object.prototype.toString.call(v);
        var i = str.indexOf(' ');
        str = str.slice(i + 1, -1);
        return str;
    
      } //typename
    
    } //apiexplorer
    
    
    //  test
    
    var mod = {
      fn: () => {},
      fn2: () => {},
      test: {
        fn3: () => {},
        fn4: () => {}
      }
    };
    
    var name = 'fn3';
    
    var list = apiexplorer(mod, 2, 'fn3');
    console.log(name, 'exists', !!list);
    
    var list = apiexplorer(mod, 2);
    console.log(JSON.stringify(list, null, 4));

    if the function does actually exists and you want to find where it is or look at etc, you can then do

    debugger;
    
    mod.test.fn3();
    

    and step into it for instance

    Login or Signup to reply.
  2. Are you looking to traverse an object recursively looking for a specific key?

    function findKey(obj, key, currentPath, visited) {
      if (obj === null || typeof obj !== 'object') {
        return
      }
    
      // prevent circular stuff
      visited = visited || []
      if (visited.includes(obj)) {
        return
      }
      visited.push(obj)
    
      currentPath = currentPath || []
    
      if (Array.isArray(obj)) {
        for (let i = 0; i < obj.length; i++) {
          findKey(obj[i], key, currentPath.concat(i), visited);
        }
      } else {
        Object.keys(obj).forEach(prop => {
          const newPath = currentPath.concat(prop);
          if (prop === key) {
            console.log(newPath.join("."));
          }
          findKey(obj[prop], key, newPath, visited);
        });
      }
    }
    
    var noop = () => {}
    
    var obj = {
      this: [{
        foo: noop
      }, {
        func: noop
      }],
      that: {
        another: 1,
        foo: noop
      }
    }
    obj.self = obj
    
    findKey(obj, 'foo')
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search