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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11279441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:37:24  来源:igfitidea点击:

Return all of the functions that are defined in a Javascript file

javascriptreflection

提问by Anderson Green

For the following script, how can I write a function that returns all of the script's functions as an array? I'd like to return an array of the functions defined in the script so that I can print a summary of every function that is defined in the script.

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

    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
    }

采纳答案by KooiInc

Declare it in a pseudo namespace, for example like this:

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

   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

回答by Ashwin Singh

Here is a function that will return all functions defined in the document, what it does is it iterates through all objects/elements/functions and displays only those whose type is "function".

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

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

? Here is a jsFiddle working demo?

? 这是一个jsFiddle 工作演示

回答by Dave Brown

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>")
 }
}