typescript 获取打字稿中的函数名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37341729/
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-10-21 03:30:13  来源:igfitidea点击:

Get name of function in typescript

javascripttypescript

提问by Mr.Trieu

I was looking for a way to get name of function passing in parameter

我正在寻找一种方法来获取传入参数的函数名称

console.clear();
class A{
   test(){


   }
   testCall(fnc:Function){
     console.log(fnc.name); // i want it display test here not empty
     console.log(fnc);

   }
}

var a=new A();
a.testCall(a.test);

you can check this in jsbin http://jsbin.com/loluhu/edit?js,console

你可以在 jsbin http://jsbin.com/loluhu/edit?js,console中查看

回答by Mr.Trieu

I found this is a bug in typescript

我发现这是打字稿中的一个错误

you can find solution here TypeScript not providing function name

你可以在这里找到解决方案 TypeScript 不提供函数名称

回答by Beevik

You can extend the Function interface as follows:

您可以按如下方式扩展 Function 接口:

interface Function {
    name: string;
}

function foo() {}
alert(foo.name);

See herefor a fuller explanation.

有关更完整的解释,请参见此处