let's say I have a function:
function test1() {
}
I want to return "test1" from within itself. I found out that you can do arguments.callee
which is going to return the whole function and then do some ugly regex. Any better way?
what about namespaced functions?
is it possible to get their name as well: e.g.:
var test2 =
{
foo: function() {
}
};
I want to return foo for this example from within itself.
update: for arguments.callee.name Chrome returns blank, IE9 returns undefined. and it does not work with scoped functions.
Method 1: Get Function Name in Python using function. func_name.
A function can refer to and call itself.
To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.
var test2 = {
foo: function() {
}
};
You aren't giving the function a name. You are assigning the foo
property of test2
to an anonymous function.
arguments.callee.name
only works when functions are declared using the function foo(){}
syntax.
This should work:
var test2 = {
foo: function foo() {
console.log(arguments.callee.name); // "foo"
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With