admin管理员组文章数量:1026640
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.
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.
Share Improve this question edited Feb 23, 2012 at 22:59 user194076 asked Feb 23, 2012 at 22:49 user194076user194076 9,03724 gold badges101 silver badges158 bronze badges 3- 1 possible dup stackoverflow./questions/2648293/… – ajax333221 Commented Feb 23, 2012 at 22:52
- possible duplicate of Does javascript function know its name and Get function name in javascript. – Felix Kling Commented Feb 23, 2012 at 22:53
- Possible duplicate of How to get the function name from within that function? – Dan Dascalescu Commented Jun 12, 2019 at 19:53
1 Answer
Reset to default 7var 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"
}
};
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.
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.
Share Improve this question edited Feb 23, 2012 at 22:59 user194076 asked Feb 23, 2012 at 22:49 user194076user194076 9,03724 gold badges101 silver badges158 bronze badges 3- 1 possible dup stackoverflow./questions/2648293/… – ajax333221 Commented Feb 23, 2012 at 22:52
- possible duplicate of Does javascript function know its name and Get function name in javascript. – Felix Kling Commented Feb 23, 2012 at 22:53
- Possible duplicate of How to get the function name from within that function? – Dan Dascalescu Commented Jun 12, 2019 at 19:53
1 Answer
Reset to default 7var 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"
}
};
本文标签: javascriptGet function name from inside itselfStack Overflow
版权声明:本文标题:javascript - Get function name from inside itself - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745649016a2161201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论