使用开发工具在复杂对象中按名称查找函数

问题描述 投票:0回答:1

我正在项目中导入库,但找不到我应该使用的方法。

函数名称是

itDoesntMatterFunctionName()

chrome 调试器工具中是否有按函数名称搜索,如果我给它一个对象,它就会找到它?

示例:

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

这个函数完全有可能没有被模块导出,因为请注意这个错误:

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

但它确实在模块中找到了其他类/对象。

Chrome 调试器/开发工具过滤器框(在对象上找不到我想要的方法或属性): enter image description here

Javascript 伪代码类似于:

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
}
javascript google-chrome-devtools
1个回答
0
投票

在我的个人库中,我将其称为 apiExplorer,它是这样的,它相当原始,可以转到版本 2,任何帮助都会很棒,例如它不检查循环引用

        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(pre===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(12);
                    var i     = str.indexOf(' ');
                    str       = str.slice(i+1,-1);
                    
              }//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));
        
        

如果该函数确实存在并且您想找到它在哪里或查看等,那么您可以这样做

debugger;

mod.test.fn3();

例如

© www.soinside.com 2019 - 2024. All rights reserved.