tags:

views:

86

answers:

3

Hello,

Does anyone know if there is a way to get javascript function name. For example I got a function like

function test1(){
alert(1);
}

I have it in my head section. Then I create an object obj1 and put my function there

obj1.func = test1;

When I call a method in obj1 object, do I have any way to get my function name (test1) inside of this method, except parsing the source (this.func.toString()) of the function.

A: 

Functions in Javascript have no name, they are first class citizens, aka objects.

those are equivalent:

function myFunc() {alert("hi");}

myFunc = function() {alert("hi");};

So you need to pass references to function objects around to be able to call them.

In your example you can call

obj1.func()

to execute the function, no need to know the name for that

Miquel
Those two are not equivalent. The first is a *function declaration* and code that appears in the source before it may call that function. In some environments, notably Firefox, `myFunc.name` will be a string containg the function name, `"myFunc"`, although this is not part of the ECMAScript spec and therefore non-standard. The second is a *function expression*, and `myFunc()` may only be called by code appearing after it in the source. Its `name` property in Firefox is `""`.
Tim Down
+3  A: 
function test() {  alert(arguments.callee.name); } 
b = test; 
b();

outputs "test" (in Chrome, Firefox and probably Safari)

If you want to get name from outside you may parse it out of:

b.toString();

but I think name property of function object might be what you need:

alert(b.name);

this however does not seem work for IE and Opera so you are left with parsing it out manually in those browsers.

Kamil Szot
Two things, this is both [deprecated](https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/arguments/callee) and doesn't work in IE, even IE8 :)
Nick Craver
@Nick Craver: `callee` was deprecated as a property of `Function.arguments`, but retained as a property of `arguments`: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Functions/arguments
Daniel Trebbien
@Nick Craver: I'm not sure why this wouldn't work in IE 8, because MSDN claims that `callee` has been available since JScript 5.5 (shipped with IE 5.5): http://msdn.microsoft.com/en-us/library/334e1zza(VS.85).aspx
Daniel Trebbien
@Daniel - First, good point about the function distinction. For the second, `JScript 5.5 != JavaScript 1.5`, it never has...but you don't have to take my word for it. Open IE8 and test it yourself: http://jsfiddle.net/guxRA/
Nick Craver
@Nick: Oh, I know that JScript 5.5 does not implement Javascript 1.5. I was just wondering why `arguments.callee.name` wouldn't work in IE 5.5+. I tested it the code in IE 6. Indeed, it doesn't work. I then tested http://jsfiddle.net/guxRA/2/ and it appears that IE 6 produces the source code for the function as `arguments.callee`. How funny.
Daniel Trebbien
@Daniel - Yup, good 'ol IE...not sure why this is getting up-voted, as it's not a viable solution to the problem, not unless you intend to ignore a very large audience. I understand if it doesn't work in IE6, I even smile a little, but IE7 and IE8 I don't think you can ignore.
Nick Craver
More to the point, the non-standard `name` property of `Function` objects is not supported in IE.
Tim Down
+2  A: 

There's no standard way to get the name of a function. Some browsers, notably Firefox and recent WebKit-based browsers, support a non-standard name property on Function objects, but no current version of IE does. The only option this leaves you is trying to parse the name from the function's string representation, which is not a good idea. There's a (long) discussion here about it: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/b85dfb2f2006c9f0

Tim Down