Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the calling function in JavaScript?

I have a function in JS which is getting called from multiple places..

Now I am testing this page on an iPad and hence find debugging a bit hard.

Can I find out by some way (print on console) from where my function is getting called from ?

like image 572
copenndthagen Avatar asked Nov 21 '11 11:11

copenndthagen


1 Answers

Like this?

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();

If you use 'use strict'; in your JavaScript file, you need to comment/remove it, because otherwise you'll get something like this:

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

like image 116
James Avatar answered Sep 30 '22 06:09

James