Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a production safe version of Function.caller in Javascript?

Is there a way to return the function that invoked the current function? Function.caller will only work for non-strict mode applications.

I want to be able to use this functionality for production environment, therefore I need strict mode to be turned on.

Expected return: function itself or the name of function.

function a() {
 b()
}

function b() {
 console.log(b.caller)
}

Function.caller will throw an error when used in strict mode:

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 361
dwen Avatar asked Oct 16 '22 11:10

dwen


1 Answers

One possible way is use Console.trace

function a() {
 b()
}

function b() {
 console.trace()
}

a()

Check the browser console to see output

like image 164
Code Maniac Avatar answered Nov 03 '22 00:11

Code Maniac