1

I'm trying to create a generic function ('displayFuncName()' for example) that I could call in different function definitions (for example 'foo()'), so it will console.log the function name ('foo' in this example). something like:

var displayFuncName = function(){
 console.log("caller function name: " + some parameter);
}

var foo = function(){
 displayFuncName(); //  should console log "caller function name: foo
}

I know it was asked in different ways in the past, but it seems like there was some deprecated option, and some of them just shows the 'displayFuncName' function name, which is of course not what I want.

user2880391
  • 2,683
  • 7
  • 38
  • 77

6 Answers6

2

Since arguments.callee throws an error in strict mode and arguments.caller is no longer supported, maybe something like that will be a better option for you:

Function.prototype.logName = function() {
    var fn = this;
    return function() {
        console.log(fn.name);
        return fn.apply(this, arguments);
    }
}

var f = function named() {}.logName();
f(); // logs `named`

In this way, you can call the logName for every function you want to log name for when it's called.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • That's nice, but it is complicated, and if I get it right - I do need to do dedicated changes in order for it to work. (like adding the 'named()', which is like writing the name of the functions twice, and adding the 'logName()' at the end). – user2880391 Sep 29 '16 at 10:46
  • As I see it it's no more of dedicated changes than putting a function call in every function you want to log a name for. _like adding the 'named()', which is like writing the name of the functions twice_ - no, it's the name of the function. `var f = function ()` - `f` is not a name here and `callee.name` won't give you `f`. `logName()` in the end works similarly like `bind` and with it you can control which functions do require logging. What's the purpose of it all? – Max Koretskyi Sep 29 '16 at 11:13
0
console.log("caller is " + arguments.callee.caller.name);
wake-0
  • 3,918
  • 5
  • 28
  • 45
0

Wouldn't it be easier to have the function name (or the function itself if you are to use theCallerFunction.name)?

This will give you the exact same results, just in a much easier way. It's also much less likely that .name will be depreciated.

var displayFuncName = function(callerFunc){
 console.log("caller function name: " + callerFunc.name);
}

var function = foo(){
 displayFuncName(foo); 
}

"arguments.callee.caller.name" is depreciated I believe.

Frogboxe
  • 386
  • 4
  • 12
0

Instead of calling a generic function from every method, you can use something like

arguments.callee.caller.name

This will give you the name of the function who called your function. If you use this in your generic method, then that will give you the name of the function in which you want to know the caller. But, if you have a common assumption that you want to know the caller of your caller, the you can use something like this.

firstFunction();

function firstFunction(){
secondFunction();
}

function secondFunction(){
whoCalled();
}

function whoCalled(){
alert("caller is " + arguments.callee.caller.arguments.callee.caller.name);
}
Prasanna
  • 46
  • 3
0

One solution would be this:

var displayFuncName = function(o) {
    console.log(o.name);
}

var foo = function(){
    displayFuncName(foo);
    console.log(foo.name); // also possible
}

foo();

But you can find way more solutions.

See here.

Community
  • 1
  • 1
Slatyoo
  • 130
  • 15
0
var displayFuncName = function(){
 console.log("caller function name: " + arguments.callee.caller.name );
}

 function foo(){
 displayFuncName(); //  should console log "caller function name: foo
}
foo();

or else if you want the whole function then you can use

var displayFuncName = function(){
 console.log("caller function name: " + arguments.callee.caller.toString());
}

var  foo = function(){
 displayFuncName(); //  should console log "caller function name: foo
}
foo();

Whichever suits your requirement.

Rohit Ailani
  • 910
  • 1
  • 6
  • 19