且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从内部获取函数名称

更新时间:2023-10-27 09:52:40

var test2 = {
   foo: function() {
   }
};

您没有给该函数命名。您正在将 test2 foo 属性分配给匿名函数。

You aren't giving the function a name. You are assigning the foo property of test2 to an anonymous function.

arguments.callee.name 仅在使用函数foo(){} 语法声明函数时有效。

arguments.callee.name only works when functions are declared using the function foo(){} syntax.

这应该有效:

var test2 = {
   foo: function foo() {
      console.log(arguments.callee.name); // "foo"
   }
};