obtain function name

M

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' );

}
 
M

Martin Honnen

Maguila007 said:
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.
 
M

Matt Kruse

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
 
G

Geoffrey Summerhayes

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'
 
M

Maguila007

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' );

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'


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?
 
M

Matt Kruse

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
 
G

Geoffrey Summerhayes

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.

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

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top