Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Javascript函数引用中获取名称作为字符串?_Javascript - Fatal编程技术网

从Javascript函数引用中获取名称作为字符串?

从Javascript函数引用中获取名称作为字符串?,javascript,Javascript,我想做与之相反的事 也就是说,考虑到: function foo() {} function bar(callback) { var name = ???; // how to get "foo" from callback? } bar(foo); 如何获取引用后面的函数名 var name = callback.name; : name属性返回函数名,或返回匿名函数的空字符串: 尝试访问.name属性: callback.name 如果无法使用,则可以: // Add a n

我想做与之相反的事

也就是说,考虑到:

function foo()
{}

function bar(callback)
{
  var name = ???; // how to get "foo" from callback?
}

bar(foo);
如何获取引用后面的函数名

var name = callback.name;
:

name属性返回函数名,或返回匿名函数的空字符串:


尝试访问
.name
属性:

callback.name 
如果无法使用,则可以:

// Add a new method available on all function values
Function.prototype.getName = function(){
  // Find zero or more non-paren chars after the function start
  return /function ([^(]*)/.exec( this+"" )[1];
};
或者对于不支持
name
属性(是否存在?)的现代浏览器,直接添加它:

if (Function.prototype.name === undefined){
  // Add a custom property to all function values
  // that actually invokes a method to get the value
  Object.defineProperty(Function.prototype,'name',{
    get:function(){
      return /function ([^(]*)/.exec( this+"" )[1];
    }
  });
}

如果您正在查找特定对象事件的函数,这可能会有帮助:

var a = document.form1
a.onsubmit.name

您可以使用以下命令提取对象和函数名:

function getFunctionName()
{
    return (new Error()).stack.split('\n')[2].split(' ')[5];
}
例如:

function MyObject()
{
}

MyObject.prototype.hi = function hi()
{
    console.log(getFunctionName());
};

var myObject = new MyObject();
myObject.hi(); // outputs "MyObject.hi"

对我来说,只需稍加修改(在父项之前添加\项),这项工作:

if (Function.prototype.name === undefined){
  // Add a custom property to all function values
  // that actually invokes a method to get the value
  Object.defineProperty(Function.prototype,'name',{
    get:function(){
      return /function ([^\(]*)/.exec( this+"" )[1];
    }
  });
}

var bat=foo;条形图(bat)
-现在它应该打印什么?@Alnitak。它应该打印
foo
。这是函数名
bat
是一个引用了
foo
函数的变量。如果回调没有名称,会发生什么情况?--ie
bar(函数(){…})
@Spudley:理想情况下,代码应该抛出一个异常,声明不支持匿名函数;尼斯:)你确定所有浏览器都会在主要的
函数之后放置空格吗?@Phrogz我有足够的信心不改变上面的内容。如果有边缘情况,它只会影响匿名函数,并且可以很容易地调整
bar
,以考虑到这一点。看到您的代码后,我记得很久以前就在规范中查看规范化字符串表示,IIRC需要空间。哦,现在我得到了
if对于
exec
match,不是regex.cool.+1.BTW的一部分,为什么它不提醒
foo)
?为什么它不匹配
foo){…
?@gdoron,因为regex必须按顺序匹配字符,直到它停止为止。regex中的
函数
启动它的匹配,然后它消耗零或更多除-a以外的任何东西-(字符数(尽可能多)。一旦完成此操作,它必须停止;它不能“跳过”不匹配项并继续。这依赖于引用函数对象的
This
。但情况并非总是如此!foo.bar()->
This
==foo例如。修改位以不包含空格:/function([^(\s]*)/这可能会起作用,但构造堆栈跟踪不是比解析
This.toString()
要昂贵得多吗?是的,但我只在调试消息时使用它。上面的答案只会返回“hi”,这返回“MyObject.hi”。这对于调试来说真的很好。谢谢或分享!
[名称]属性不标准
不再正确,它在ECMA 6.0/2015中:/另请参见
function MyObject()
{
}

MyObject.prototype.hi = function hi()
{
    console.log(getFunctionName());
};

var myObject = new MyObject();
myObject.hi(); // outputs "MyObject.hi"
if (Function.prototype.name === undefined){
  // Add a custom property to all function values
  // that actually invokes a method to get the value
  Object.defineProperty(Function.prototype,'name',{
    get:function(){
      return /function ([^\(]*)/.exec( this+"" )[1];
    }
  });
}