返回在 Javascript 文件中定义的所有函数

IT技术 javascript reflection
2021-02-16 19:08:28

对于以下脚本,我如何编写一个函数,将脚本的所有函数作为数组返回?我想返回脚本中定义的函数数组,以便我可以打印脚本中定义的每个函数的摘要。

    function getAllFunctions(){ //this is the function I'm trying to write
        //return all the functions that are defined in the script where this
        //function is defined.
        //In this case, it would return this array of functions [foo, bar, baz,
        //getAllFunctions], since these are the functions that are defined in this
        //script.
    }

    function foo(){
        //method body goes here
    }

    function bar(){
        //method body goes here
    }

    function baz(){
        //method body goes here
    }
4个回答

这是一个函数,它将返回文档中定义的所有函数,它的作用是遍历所有对象/元素/函数,并仅显示类型为“函数”的那些。

function getAllFunctions(){ 
        var allfunctions=[];
          for ( var i in window) {
        if((typeof window[i]).toString()=="function"){
            allfunctions.push(window[i].name);
          }
       }
    }

这里是一个的jsfiddle工作演示

这样做的问题是它会捕获在以前的脚本中定义的所有附加到 window 的函数。首先将东西扔到窗户上是一个非常糟糕的主意。
2021-04-15 19:08:28
这应该在今天的浏览器中仍然有效吗?我将它粘贴到 DevTools Console 中,它返回“未定义”
2021-04-17 19:08:28
添加&&window[i].toString().indexOf("native")==-1到 if 修复了这个问题。
2021-04-29 19:08:28
@golimar 它返回undefined是因为它没有return声明。在这里再次测试了它,它仍然生成一个函数名称列表。
2021-05-14 19:08:28

在伪命名空间中声明它,例如像这样:

   var MyNamespace = function(){
    function getAllFunctions(){ 
      var myfunctions = [];
      for (var l in this){
        if (this.hasOwnProperty(l) && 
            this[l] instanceof Function &&
            !/myfunctions/i.test(l)){
          myfunctions.push(this[l]);
        }
      }
      return myfunctions;
     }

     function foo(){
        //method body goes here
     }

     function bar(){
         //method body goes here
     }

     function baz(){
         //method body goes here
     }
     return { getAllFunctions: getAllFunctions
             ,foo: foo
             ,bar: bar
             ,baz: baz }; 
    }();
    //usage
    var allfns = MyNamespace.getAllFunctions();
    //=> allfns is now an array of functions. 
    //   You can run allfns[0]() for example
+1 正确捕获函数,我相信我在 John Resig 的JavaScript Ninja秘密书中看到过类似的方式
2021-04-18 19:08:28
它有效,但这似乎有点多余: return { getAllFunctions: getAllFunctions ,foo: foo ,bar: bar ,baz: baz }; 是否可以在不硬编码每个函数的名称的情况下做到这一点?
2021-04-26 19:08:28
@Anderson:您需要对函数本身的引用,但它不必是公开的。或者,您可以在其中分配一个像var fns = {foo:foo,bar:bar ...}变量这样的对象MyNamespae并使用getAllFunctions. 比你只需要暴露getAllFunctions- >return {getAllfunctions:getAllfunctions};
2021-05-09 19:08:28
如果我在这里使用 Angular.js 注释函数:github.com/angular/angular.js/blob/... annotate(allfns[0])我得到一个[]参数,同时它调用实际函数annotate(MyNamespace. foo)为什么?
2021-05-12 19:08:28

function foo(){/*SAMPLE*/}
function bar(){/*SAMPLE*/}
function www_WHAK_com(){/*SAMPLE*/}

for(var i in this) {
	if((typeof this[i]).toString()=="function"&&this[i].toString().indexOf("native")==-1){
		document.write('<li>'+this[i].name+"</li>")
	}
}

浪费了1个多小时。

这是.js读取文件node.js

1.安装节点module:

npm i esprima

2.假设您在当前目录func1的文件a.js声明了一个函数,如下所示

var func1 = function (str1, str2) {
    //
};

3.你想得到它的名字,即func1,代码如下:

const fs = require("fs");
const esprima = require("esprima");

let file = fs.readFileSync("./a.js", "utf8");

let tree = esprima.parseScript(file);
tree.body.forEach((el) => {
    if (el.type == "VariableDeclaration") {
        // console.log(el);
        console.log(el.declarations);
        console.log(el.declarations[0].id);
        console.log(el.declarations[0].id.name);
    }
});

4.您还可以获取其他详细信息,例如参数str1str2等,取消注释该console.log(el)行以查看其他详细信息。

5.您可以将上述两个代码部分放在一个文件中,以获取当前文件的详细信息(a.js)。