364,083 Members | 5476 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

obtain function name

Maguila007
P: n/a
Maguila007
Hi

Is there any way to obtain the name of the function, inside the
function which was called.

Ex:

function something() {

alert( "The name of the function you invoke is " ......should
says 'something' );

}

May 2 '07 #1
Share this Question
Share on Google+
6 Replies


Martin Honnen
P: n/a
Martin Honnen
Maguila007 wrote:
Is there any way to obtain the name of the function, inside the
function which was called.
function something() {
>
alert( "The name of the function you invoke is " ......should
says 'something' );
arguments.callee
gives you the function itself, in some implementations (e.g. Mozilla
JavaScript) functions have a name property so doing
alert(arguments.callee.name);
should do, otherwise you have to call toString() on the function object
and parse out the function name.




--

Martin Honnen
http://JavaScript.FAQTs.com/
May 2 '07 #2

Matt Kruse
P: n/a
Matt Kruse
On May 2, 10:28 am, Maguila007 <leonemat...@gmail.comwrote:
Is there any way to obtain the name of the function, inside the
function which was called.
No, because think about it - a function doesn't even need to have a
name. Or it can be referenced by 5 different names. A function is just
an object, whereas the name is one of potentially many references to
the object.

var func1 = func2 = func3 = function() { alert("what is my name?"); };

(function(){ alert("I don't even have a name"); })();

In simple cases, using arguments.callee may work, but it's by no means
a robust or dependable solution.

Matt Kruse

May 2 '07 #3

Geoffrey Summerhayes
P: n/a
Geoffrey Summerhayes
On May 2, 12:28 pm, Maguila007 <leonemat...@gmail.comwrote:
Hi
>
Is there any way to obtain the name of the function, inside the
function which was called.
>
Ex:
>
function something() {
>
alert( "The name of the function you invoke is " ......should
says 'something' );
>
}
Sort of..

function functionName(fn)
{
var name=/\W*function\s+([\w\$]+)\(/.exec(fn);
if(!name)return 'No name';
return name[1];
}

function foo(){alert(functionName(foo))}

Of course, it would be easier to write:

function foo(){alert('foo')}

and get the same effect.

Also

var bar=foo;
foo(); ->gives 'foo'
bar(); ->gives 'foo'

and

var foo=function(){alert(functionName(foo))};
var bar=foo;
foo(); -gives 'No name'
bar(); -gives 'No name'

----
Geoff

May 2 '07 #4

Maguila007
P: n/a
Maguila007
On May 2, 2:16 pm, Geoffrey Summerhayes <sumr...@gmail.comwrote:
On May 2, 12:28 pm, Maguila007 <leonemat...@gmail.comwrote:
>
Hi
>
Is there any way to obtain the name of the function, inside the
function which was called.
>
Ex:
>
function something() {
>
alert( "The name of the function you invoke is " ......should
says 'something' );
>
}
>
Sort of..
>
function functionName(fn)
{
var name=/\W*function\s+([\w\$]+)\(/.exec(fn);
if(!name)return 'No name';
return name[1];
>
}
>
function foo(){alert(functionName(foo))}
>
Of course, it would be easier to write:
>
function foo(){alert('foo')}
>
and get the same effect.
>
Also
>
var bar=foo;
foo(); ->gives 'foo'
bar(); ->gives 'foo'
>
and
>
var foo=function(){alert(functionName(foo))};
var bar=foo;
foo(); -gives 'No name'
bar(); -gives 'No name'
>
----
Geoff

But I need to know "what is my name" inside the function, I do not
have a variable pointing to the function.
I tested the callee.name and it´s work great for my porpouse in
Mozilla.
Is there anything like that for IE and Opera?

May 2 '07 #5

Matt Kruse
P: n/a
Matt Kruse
On May 2, 12:09 pm, Maguila007 <leonemat...@gmail.comwrote:
But I need to know "what is my name" inside the function, I do not
have a variable pointing to the function.
You do, but you just don't realize it.
Functions don't have names. When you say:

function foo() { }

you are just creating a function object, and assigning a reference to
it by the name of 'foo'. The function object itself knows nothing
about 'foo' - it's just the name you as a programmer gave it so you
could refer to it.

That may be getting a little deeper than you really are interested in,
but in order to really answer the question you need to understand how
this stuff works and why you may never get a solution that you're
happy with.

Matt Kruse

May 2 '07 #6

Geoffrey Summerhayes
P: n/a
Geoffrey Summerhayes
On May 2, 2:09 pm, Maguila007 <leonemat...@gmail.comwrote:
On May 2, 2:16 pm, Geoffrey Summerhayes <sumr...@gmail.comwrote:
>
>
>
>
>
On May 2, 12:28 pm, Maguila007 <leonemat...@gmail.comwrote:
>
Hi
>
Is there any way to obtain the name of the function, inside the
function which was called.
>
Ex:
>
function something() {
>
alert( "The name of the function you invoke is " ......should
says 'something' );
>
}
>
Sort of..
>
function functionName(fn)
{
var name=/\W*function\s+([\w\$]+)\(/.exec(fn);
if(!name)return 'No name';
return name[1];
>
}
>
function foo(){alert(functionName(foo))}
>
Of course, it would be easier to write:
>
function foo(){alert('foo')}
>
and get the same effect.
>
Also
>
var bar=foo;
foo(); ->gives 'foo'
bar(); ->gives 'foo'
>
and
>
var foo=function(){alert(functionName(foo))};
var bar=foo;
foo(); -gives 'No name'
bar(); -gives 'No name'
>
----
Geoff
>
But I need to know "what is my name" inside the function, I do not
have a variable pointing to the function.
Of course you do. To work it requires the function to be written in
the form 'function *name* () {} so using *name* inside automatically
refers to the function unless you override it with a variable
definition.
If you use an obfuscation program on it, it should alter both to the
same to be correct.
I tested the callee.name and it´s work great for my porpouse in
Mozilla.
Is there anything like that for IE and Opera?-
arguments.callee initially contains the function body being executed
(ref ECMA 262 '97)
You'll have to extract the function name from there.

----
Geoff



May 2 '07 #7

Post your reply

Help answer this question



Didn't find the answer to your JavaScript / Ajax / DHTML question?

You can also browse similar questions: JavaScript / Ajax / DHTML