473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

obtain function name

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
6 41046
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
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
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
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
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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sylvain Provencher | last post by:
How can I obtain function name at runtime ? example : to display the name of the function that cause an error
4
by: Gnanaprakash Rathinam | last post by:
Hi Expert, Is there a way to obtain assembly name in an unmanaged call? During Interop call between managed to unmanaged, I would like to know in unmanaged code about the caller of assembly file...
11
by: babuyama | last post by:
Hi, Is there a way to obtain library name at compile/preprocessor time? Assuming that the compilation unit, myfile.c is part of mylib.a, from myfile.c code at compile/preprocessor time, I would...
5
by: OneFang | last post by:
Hi I hope I make sense here. I want to be able to obtain the name of the method that calls a method within my class. So If I have my class that has a method LogInfo() And my client code...
4
by: vcinquini | last post by:
How to obtain a function name during the execution code? For example: public void Acme() { try { some code goes here }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.