Can I get the name of the function using the variable that is present inside the function?
function abcd() {
var temp;
alert(" name of the function using temp ");
};
How do I get the name of the function 'abcd'
using temp?
Can I get the name of the function using the variable that is present inside the function?
function abcd() {
var temp;
alert(" name of the function using temp ");
};
How do I get the name of the function 'abcd'
using temp?
You can use arguments.callee.name
, like this
function abcd() {
console.log("name of the function using", arguments.callee.name);
};
abcd();
# name of the function using abcd
Note: arguments.callee.name
will work only with the functions defined with a name. It will not work with anonymous functions. For example, if you had defined your function, like this
var abcd = function () {
console.log("name of the function using", arguments.callee.name);
};
abcd()
would just print name of the function using
. Because arguments.callee.name
would be an empty string in this case.
In "non strict" mode you can use arguments.callee.name
.
But in general you shouldn't - it's unsafe to assume anything about the variable(s) an object or function is known by from within because there can be more than one, or in the case of an anonymous function no name at all.