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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10624057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:12:30  来源:igfitidea点击:

Get name as String from a Javascript function reference?

javascript

提问by Gili

I want to do the opposite of Get JavaScript function-object from its name as a string?

我想做与从名称获取 JavaScript 函数对象相反的字符串作为字符串?

That is, given:

也就是说,给定:

function foo()
{}

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

bar(foo);

How do I get the name of the function behind a reference?

如何获取引用背后的函数名称?

采纳答案by Phrogz

If you can't use myFunction.namethen you can:

如果你不能使用,myFunction.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];
};

Or for modern browsers that don't support the nameproperty (do they exist?) add it directly:

或者对于不支持该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];
    }
  });
}

回答by gdoron is supporting Monica

var name = callback.name;

MDN:

目录

The name property returns the name of a function, or an empty string for anonymous functions:

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

Live DEMO

现场演示

回答by kojiro

function bar(callback){
    var name=callback.toString();
    var reg=/function ([^\(]*)/;
    return reg.exec(name)[1];
}

>>> function foo() { };
>>> bar(foo);
"foo"
>>> bar(function(){});
""

回答by David Fooks

You can extract the object and function name with:

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

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

For example:

例如:

function MyObject()
{
}

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

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

回答by osahyoun

var x = function fooBar(){};
console.log(x.name);
// "fooBar"

回答by Tiago Peczenyj

try to access the .nameproperty:

尝试访问该.name属性:

callback.name 

回答by JD Philippe

for me, with just a little modification (adding \ before parent), this work:

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

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];
    }
  });
}

回答by Pipe2290

If you were looking for the function on an specific object event, this may help:

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

var a = document.form1
a.onsubmit.name