2012-07-01 19 views
8

Per il seguente script, come posso scrivere una funzione che restituisce tutte le funzioni dello script come una matrice? Mi piacerebbe restituire una serie di funzioni definite nello script in modo che possa stampare un riepilogo di ogni funzione definita nello script.Restituisce tutte le funzioni definite in un file Javascript

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 
    } 
+0

Per chiarire, getAllFunctions() dovrebbe solo re trasforma le funzioni che sono definite nello script stesso e nient'altro. –

+0

Ho chiarito la domanda originale in modo che la domanda non sia più ambigua. –

+0

Vedere questo post http://stackoverflow.com/questions/493833/list-of-global-user-defined-functions-in-javascript – anazimok

risposta

9

dichiararlo in una pseudo spazio dei nomi, per esempio come questo:

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 
+1

+1 per le funzioni a ragione la cattura, credo che avevo visto in modo simile Segreti ** di John Resig di JavaScript Ninja ** libro – Blaster

+0

funziona, ma questo sembra un po 'ridondante: ritorno {getAllFunctions: getAllFunctions , pippo: pippo , bar: bar , baz: baz}; È possibile farlo senza codificare il nome di ogni funzione? –

+0

@Anderson: è necessario un riferimento alle funzioni stesse, ma non deve essere pubblico. In alternativa puoi assegnare un oggetto come la variabile 'var fns = {foo: foo, bar: bar ...}' in 'MyNamespae' e interrogare l'argomento usando' getAllFunctions'. Quindi devi solo esporre 'getAllFunctions' ->' return {getAllfunctions: getAllfunctions}; ' – KooiInc

10

Ecco una funzione che restituisce tutte le funzioni definite nel documento, ciò che fa è che scorre tutti oggetti/elementi/funzioni e visualizza solo quelli il cui tipo è "funzione".

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

Ecco un jsFiddle working demo

+0

Il problema con questo è che catturerà tutte le funzioni definite negli script precedenti che sono stati allegati alla finestra. Ed è una pessima idea gettare le cose sulla finestra, in primo luogo. –

+2

Aggiunta di '&& window [i] .toString(). IndexOf (" native ") == - 1' a if se lo risolve. –

1

function foo(){/*SAMPLE*/} 
 
function bar(){/*SAMPLE*/} 
 
function www_WHAK_com(){/*SAMPLE*/} 
 

 
for(var i in this) { 
 
\t if((typeof this[i]).toString()=="function"&&this[i].toString().indexOf("native")==-1){ 
 
\t \t document.write('<li>'+this[i].name+"</li>") 
 
\t } 
 
}

Problemi correlati