Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  167] [ 1]  / answers: 1 / hits: 48279  / 9 Years ago, sat, april 11, 2015, 12:00:00

Is it possible to see the callee/caller of a function when use strict is enabled?





'use strict';

function jamie (){
console.info(arguments.callee.caller.name);
//this will output the below error
//uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
};

function jiminyCricket (){
jamie();
}

jiminyCricket ();




More From » use-strict

 Answers
3

For what it's worth, I agree with the comments above. For whatever problem you're trying to solve, there are usually better solutions.



However, just for illustrative purposes, here's one (very ugly) solution:



'use strict'

function jamie (){
var callerName;
try { throw new Error(); }
catch (e) {
var re = /(w+)@|at (w+) (/g, st = e.stack, m;
re.exec(st), m = re.exec(st);
callerName = m[1] || m[2];
}
console.log(callerName);
};

function jiminyCricket (){
jamie();
}

jiminyCricket(); // jiminyCricket


I've only tested this in Chrome, Firefox, and IE11, so your mileage may vary.


[#67113] Thursday, April 9, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jameson

Total Points: 534
Total Questions: 103
Total Answers: 102

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
jameson questions
;