2011-11-21 14 views
8

chiamando Ho una funzione in JS, che è sempre chiamato da più luoghi ..Javascript trovare funzione

ora sto testando questa pagina su un iPad e quindi trovare il debug di un po 'difficile.

Posso sapere in qualche modo (stampare su console) da dove viene richiamata la funzione?

+2

Ciò contribuirà: http://stackoverflow.com/questions/280389/how-do-you-find-out-the-caller-function-in-javascript – Sid

+0

Un altro link http://stackoverflow.com/questions/147891/javascript-exception-stack-trace –

risposta

13

Ti piace questo?

function testOne() { 
    console.log("Test 1"); 
    logTest(); 
} 
function testTwo() { 
    console.log("Test 2"); 
    logTest(); 
} 
function logTest() { 
    console.log("Being called from " + arguments.callee.caller.toString()); 
} 

testOne(); 
testTwo(); 

Se si utilizza 'use strict'; nel file JavaScript, è necessario lasciare un commento/rimuoverlo, perché altrimenti si otterrà qualcosa di simile:

Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

+3

Sto ricevendo un errore o .... Risultato dell'espressione arguments.callee.caller non è un oggetto – testndtv

+0

Sto verificando questo su iPad Safari..se questo fa differenza ... – testndtv

+0

@James, +1, La tua funzione ha salvato il mio tempo per capire la struttura esistente e completare il mio lavoro. Grazie molto!!! –

9

Un modo semplice mi piace usare è arguments.callee.caller.name .

detto che volevi sapere cosa stava chiamando una funzione denominata myFunction:

function myFunction() { 
    console.log(arguments.callee.caller.name); 
    /* Other stuff... */ 
} 

Il supporto del browser per questo che non è grande, però, così si potrebbe usare arguments.callee.caller.toString() anziché. Tieni presente che questo ti restituirà il contenuto della funzione che ha chiamato myFunction, quindi dovrai estrarre il nome della funzione da te stesso.

Oppure, si potrebbe utilizzare una bella funzione dello stack trace in questo modo:

function getStack(){ 
    fnRE = /function\s*([\w\-$]+)?\s*\(/i; 
    var caller = arguments.callee.caller; 
    var stack = "Stack = "; 
    var fn; 
    while (caller){ 
     fn = fnRE.test(caller.toString()) ? RegExp.$1 || "{?}" : "{?}"; 
     stack += "-->"+fn; 
     caller = caller.arguments.callee.caller; 
    }; 
    return stack; 
} 

Stack Trace via http://www.amirharel.com/2010/01/25/using-caller-and-callee-for-stack-trace/

0

Vuoi Particolare su Function chiamante:

function nishant(){ // Caller function 
    kumar(); 
}nishant(); 

function kumar(){ // Callee 
console.log("This functiona is being called by " + arguments.callee.caller.toString()); 
} 

Al posto di arguments.callee.caller.toString() possiamo anche usare functionName.caller

Esempio:

function nishant(){ // Caller function 
    kumar(); 
}nishant(); 

function kumar(){ // Callee 
console.log("This functiona is being called by " + kumar.caller); 
} 

uscita: sarà stesso in entrambi caso precedente

This functiona is being called by function nishant() 
{ 
kumar(); 
} 
+0

* functionName.caller * è bello, ma [non standard] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller) (secondo MDN) – mTorres