10

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

Mr.Trieu
  • 414
  • 2
  • 6
  • 16
  • 1
    Have a look at this http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript It explains that although there is no real reflection in javascript, you can still use some workarounds but they are not very robust. – zoubida13 May 20 '16 at 08:34
  • 4
    Possible duplicate of [Get name as String from a Javascript function reference?](http://stackoverflow.com/questions/10624057/get-name-as-string-from-a-javascript-function-reference) – Starfish May 20 '16 at 08:35
  • Possible duplicate of [TypeScript not providing function name](http://stackoverflow.com/questions/33647589/typescript-not-providing-function-name) – Alex May 20 '16 at 09:16

2 Answers2

3

I found this is a bug in typescript

you can find solution here TypeScript not providing function name

Community
  • 1
  • 1
Mr.Trieu
  • 414
  • 2
  • 6
  • 16
2

You can extend the Function interface as follows:

interface Function {
    name: string;
}

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

See here for a fuller explanation.

Beevik
  • 540
  • 6
  • 11