2

From 3 hours, I search for make a Javascript function who return the list of reachable/created functions by an user.

Unfortunately, I don't find anything....

You know how to do this on google V8 Javascript engine ?

PS: Sorry for my english, I'm french student :D

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Hannibal
  • 21
  • 1
  • 5

3 Answers3

2

There is no programmatical way of scraping a list of current functions or variables.

You would in the least need to know where these functions lived - e.g. on a particular namespace.

var fn = {};
fn.someFunc = function(){}
fn.someProp = 'foo';
fn.someOtherFunc = function(){}

//get all funcs
for (var i in fn) if (typeof fn[i] == 'function') alert('found a function');
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • How come the chrome console can do it then? – starbeamrainbowlabs Aug 03 '12 at 10:47
  • 2
    Chrome's web tools are not purely JS. – Mitya Aug 03 '12 at 10:48
  • That is so bad... because, my functions are global. I integrate V8 engine into C++ software so that users can create their scripts – Hannibal Aug 03 '12 at 10:49
  • 1
    In fairness, this is not a great pattern - trying to scrape together a list of things made. JS does not work like that. You would be better to force your users to create functions on a particular namespace, as I demonstrated. – Mitya Aug 03 '12 at 11:03
  • 1
    @Hannibal, `this` is the global object on the top level. – katspaugh Aug 03 '12 at 11:05
  • 1
    Thanks katspaught, I found a solution. `function LISTFUNCT(){ var liste=''; for(var funct in this){ if(funct.lengt>1) liste+=funct+', '; } liste=liste.substr(0,liste.length-2); return liste; }` That is not perfect but that is enough – Hannibal Aug 03 '12 at 11:18
  • I found a new version: This version return all functions (without variable) function LISTFUNCT(){ var liste=''; for(var funct in this){ if(funct.length>1) if(typeof(this.eval(funct))=='function') liste+=funct+', '; } liste=liste.substr(0,liste.length-2); return liste; } Enjoy :D – Hannibal Aug 03 '12 at 14:07
  • @Utkanos You can inspect the inspector and see – starbeamrainbowlabs Aug 03 '12 at 15:29
2

Please note that by default the functions which are not declared as a method of an object are considered as a method of window object.

So, using something like the code below you can list all functions of window object. You can inspect the output and add better filters to improve isFunction function which I steal here.

<script type="text/javascript">

        function myTest() //function to be detected
        {
            return true;
        }

        function isFunction(functionToCheck)
        {
            var getType = {};
            return functionToCheck && getType.toString.call(functionToCheck) == '[object Function]';
        }

        for( var it in window )
        {
            if(isFunction(window[it]))
                console.log(it+":"+window[it]);
        }


    </script>
Community
  • 1
  • 1
Mikhail Payson
  • 923
  • 8
  • 12
  • window Object is not defined in V8 engine (tested in Node.js prompt command line) – Hannibal Aug 03 '12 at 10:54
  • Hm... Sounds logically there are no windows on server side... So we need to understand which object are the functions attached to by default – Mikhail Payson Aug 03 '12 at 11:00
  • 2
    *Please note that by default the functions which are not declared as a method of an object are considered as a method of window object.* - true of global functions but not those declared inside closures. They are declared on the local scope chain. – Mitya Aug 03 '12 at 11:01
1

Maybe I can help you. This call go top to down in every object, starting with global context. the first function return the global object that is where are our free-scope objects . And we do a recursion over each object search for functions or anything you want.

var global = (function(){return this;})();

(function(ctx){
  for(var i in ctx) { 
   // put your function check here.
   // System.out.println(i + ' ' + typeof ctx[i]); 
   if(ctx[i] instanceof Object) { 
     arguments.callee(ctx[i]); }
  }
})(global);

run code on the fly

heat
  • 1,295
  • 9
  • 20