0

I have a javascript file and in that there are some functions defined in it. How can i get all the function name defined in that particular js file in an array of string

  function getabc1() {
    //block of code
  }
  function getabc2() {
        //block of code
  }
  function getabc3() {
        //block of code
  }
  function getabc4() {
        //block of code
  }
  function getabc5() {
       //block of code
  }
  function getabc6() {
       //block of code
  }
  function getabc7() {
       //block of code
  }

I need all the 7 functions defined in this particular js file. Only name of function It will be great help if someone can help me on this.

  • already have looked this thing......but the problem is it is returning the code inside the functions as well.....which i dnt need i just need the name of functions getabc1, getabc2, getabc3, getabc4, getabc5, getabc6, getabc7 – PRASHANT TRIPATHI Aug 27 '19 at 10:59
  • 1
    @PRASHANTTRIPATHI you're just gonna inspect the code a little bit, SO answers aren't always meant to give you the exact thing you need. You should learn to derive from it. Lookin at the link @AndroidNoobie said, all you had to do is get the key `l` instead of the entire value `this[l]`. – Abana Clara Aug 27 '19 at 11:03

1 Answers1

1

You're just gonna inspect the code a little bit, answers in Stackoverflow aren't always meant to give you the exact thing you need. You should learn to derive from it. Looking at the link @AndroidNoobie said, all you had to do is push the key instead of the entire iterated value.

As stated on https://stackoverflow.com/a/11279959/7325182:

Declare it in a pseudo namespace

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(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();
console.log(allfns)
//=> allfns is now an array of functions. 
//   You can run allfns[0]() for example
Abana Clara
  • 4,602
  • 3
  • 18
  • 31