Forum Moderators: open
function abc()
{
foo("Hello");
}
function xyz()
{
foo("Bye");
}
function foo(msg)
{
// Print "function: msg"
}
abc();
xyz();
Desired output:
foo(): Hello
xyz(): Bye
I don't know if this is possible. Ideally, I would like not to have to pass another variable to foo containing the function name... I just want it to dynamically "know" who called it. This is probably impossible, but thought I'd ask.
Thanks.
I suppose you could define a global variable and populate it with the name of the function:
var lastFunc=''
function abc()
{
lastFunc='abc';
foo("Hello");
}
At least that way you won't have to pass the function name as a parameter.
Of course, 'deprecated' just means that they discourage you from using it, but it will still work. Also, there is no current replacement as far as I know.
ajkimoto
function abc()
{
foo("Hello",arguments.callee);
}
function xyz()
{
foo("Bye",arguments.callee);
}
function foo(msg)
{
if( arguments.length > 1 )
{
// Parse the function name and append it
}
// Print "function: msg"
}
I had hoped to avoid having to pass an argument, but there seems to be no good way around it. At least this way I can just use arguments.callee and I don't have to worry about the function names changing.