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

前端 未结 4 1901
孤独总比滥情好
孤独总比滥情好 2020-11-27 19:53

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 sc

4条回答
  •  星月不相逢
    2020-11-27 20:17

    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 ​

提交回复
热议问题