4

I have the following code snippet

function receiver(callback)
{
    console.log( callback );
}

function callback(){}

receiver( new callback() );

OUTPUT: callback {}

is there a method or way to get 'callback' out of callback parameter? I like to get an object's name.

Moon
  • 22,195
  • 68
  • 188
  • 269
  • 1
    Check out this question: http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript – Cyclonecode Jan 22 '12 at 09:15

2 Answers2

7
> function callback(){}
undefined
> a = new callback();
[object Object]
> a.constructor.name
callback>

But, it won't work for any anonymous functions (everything is in the title):

> callback = function(){};
function () {}
> c = new callback();
[object Object]
> c.constructor.name
(empty string)
greut
  • 4,305
  • 1
  • 30
  • 49
2

Try:

function receiver(callback){
    console.log(callback.constructor.name);
}

function callback(){}

receiver(new callback());

Have a look at: javascript introspection in 90 seconds

ghstcode
  • 2,902
  • 1
  • 20
  • 30