21

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
    }
9
  • To clarify, getAllFunctions() should only return the functions that are defined in the script itself, and nothing else. Commented Jul 1, 2012 at 3:58
  • I clarified the original question so that the question is no longer ambiguous. Commented Jul 1, 2012 at 4:00
  • See this post stackoverflow.com/questions/493833/… Commented Jul 1, 2012 at 4:01
  • 1
    I'm not sure if that's an exact duplicate, since the asker of that question only wanted to filter out "native functions" as opposed to "user-defined functions." That question is close to what I'm looking for, but the question is not an exact match for my question. Commented Jul 1, 2012 at 4:05
  • 1
    What exactly is the purpose of this function, are you unable to define the functions on their own object - which would make detection of them far easier. I'm failing to understand the purpose behind the "getAllFunctions" function. Commented Jul 1, 2012 at 5:53

4 Answers 4

19

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.

​Add the function at the last and this snippet getAllFunctions().slice(48, -4) will just return the user defined functions in Vivaldi.

Sign up to request clarification or add additional context in comments.

4 Comments

The problem with this is that it will catch all functions defined in previous scripts that were attached to window. And it's a very bad idea to toss things onto window in the first place.
Adding &&window[i].toString().indexOf("native")==-1 to the if fixes that.
Should this still work in today's browsers? I paste it into DevTools Console and it returns 'undefined'
@golimar It returns undefined because it doesn't have a return statement. I tested it again here, and it still generates a list of function names.
13

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

4 Comments

+1 for rightly capturing functions, I believe I had seen similar way in John Resig's Secrets of JavaScript Ninja book
It works, but this seems a bit redundant: return { getAllFunctions: getAllFunctions ,foo: foo ,bar: bar ,baz: baz }; Is it possible to do this without hard-coding the name of each function?
@Anderson: you need a reference to the functions themselves, but it doesn't have to be public. Alternatively you can assign an object like var fns = {foo:foo,bar:bar ...} variable within MyNamespae and query that using getAllFunctions. Than you only have to expose getAllFunctions -> return {getAllfunctions:getAllfunctions};
If I use the Angular.js annotate function here: github.com/angular/angular.js/blob/… annotate(allfns[0]) I get a [] of parameters, while it works calling the actual function annotate(MyNamespace. foo). Why?
3

More than 1 hour wasted on this.

This is read .js file from node.js

1.Install a node module:

npm i esprima

2.Assume you have a function func1 declared like below, in a file a.js in current directory:

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

3.And you would like to get name of it, that is func1, the code is below:

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.You can also get other details like parameters str1, str2, etc, uncomment the console.log(el) line to see other details.

5.You can put both above code parts in one file, to get the details of current file (a.js).

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.