How to Get the Name of the Currently Running Function in JavaScript

Can I get the name of the currently running function in JavaScript?

In ES5 and above, there is no access to that information.

In older versions of JS you can get it by using arguments.callee.

You may have to parse out the name though, as it will probably include some extra junk. Though, in some implementations you can simply get the name using arguments.callee.name.

Parsing:

function DisplayMyName() 
{
var myName = arguments.callee.toString();
myName = myName.substr('function '.length);
myName = myName.substr(0, myName.indexOf('('));

alert(myName);
}

Source: Javascript - get current function name.

Get current function name in strict mode

For logging/debugging purposes, you can create a new Error object in the logger and inspect its .stack property, e.g.

function logIt(message) {    var stack = new Error().stack,        caller = stack.split('\n')[2].trim();    console.log(caller + ":" + message);}
function a(b) { b()}
a(function xyz() { logIt('hello');});

get the current function name in javascript

This -

var my_arguments;

function MyFunction() {
my_arguments = arguments;

$.ajax({
type: "POST",
url: "http://www.google.com",
dataType: "json",
data: "foo=bar",
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert(my_arguments.callee.name);
},
success: function(jsonObject) {
//do something
}
});
}

is what you need.

The arguments inside the error function refers to this method's own arguments object. It does not refer to the MyFunction's arguments object. That's why you are getting error:MyFunction. Using a global variable in this case provides you a workaround to this problem.

Also, to get only the name of the function, you need to use arguments.callee.name. arguments.callee will give you a reference to the calling function, not a function name in string.

How can I get the name of function inside a JavaScript function?

I think that you can do that :

var name = arguments.callee.toString();

For more information on this, take a look at this article.

function callTaker(a,b,c,d,e){
// arguments properties
console.log(arguments);
console.log(arguments.length);
console.log(arguments.callee);
console.log(arguments[1]);
// Function properties
console.log(callTaker.length);
console.log(callTaker.caller);
console.log(arguments.callee.caller);
console.log(arguments.callee.caller.caller);
console.log(callTaker.name);
console.log(callTaker.constructor);
}

function callMaker(){
callTaker("foo","bar",this,document);
}

function init(){
callMaker();
}

Get function name in JavaScript

function test() {  alert(arguments.callee.name); } 
b = test;
b();

outputs "test" (in Chrome, Firefox and probably Safari). However, arguments.callee.name is only available from inside the function.

If you want to get name from outside you may parse it out of:

b.toString();

but I think name property of function object might be what you need:

alert(b.name);

this however does not seem work for IE and Opera so you are left with parsing it out manually in those browsers.

How to get function name in strict mode [proper way]

Is there any new proper (standard) alternative for getting function name inside actual function?

No, there is not.

Or will it be in future plans for ES?

No, given that there is no need for it. Inside the current function, you know the name and could just as well use a string literal, in other functions you just need some reference (but not .callee).

Get method name from within a typescript method

Besides the arguments.callee.name there is no straightforward way of getting this.

I propose 2 other methods:

Use decorators to inject the method name:

function annotateName(target, name, desc) {
var method = desc.value;
desc.value = function () {
var prevMethod = this.currentMethod;
this.currentMethod = name;
method.apply(this, arguments);
this.currentMethod = prevMethod;
}
}

class Foo {
currentMethod: string;

@annotateName
bar() {
alert(this.currentMethod);
this.tux();
alert(this.currentMethod);
}

@annotateName
tux() {
alert(this.currentMethod);
}
}

new Foo().bar();

The downside is that you have to annotate all the functions you want to get the name from. You could instead just annotate the class and in the decorator you would iterate over all prototype functions and apply the same idea.


My second option is not standardised and need more care to get consistent results across browsers. It relies on creating an Error object and getting it's stack trace.

class Foo {
bar() {
console.log(getMethodName());
}
}

function getMethodName() {
var err = new Error();
return /at \w+\.(\w+)/.exec(err.stack.split('\n')[2])[1] // we want the 2nd method in the call stack

}

new Foo().bar();

How do you find out the caller function in JavaScript?

Note that this solution is deprecated and should no longer be used according to MDN documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller



function Hello()
{
alert("caller is " + Hello.caller);
}

Note that this feature is non-standard, from Function.caller:

Non-standard

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.


The following is the old answer from 2008, which is no longer supported in modern Javascript:

function Hello()
{
alert("caller is " + arguments.callee.caller.toString());
}


Related Topics



Leave a reply



Submit