Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an object name? [duplicate]

Tags:

javascript

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.

like image 991
Moon Avatar asked Jan 17 '23 17:01

Moon


2 Answers

> 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)
like image 107
greut Avatar answered Jan 29 '23 09:01

greut


Try:

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

function callback(){}

receiver(new callback());

Have a look at: javascript introspection in 90 seconds

like image 23
ghstcode Avatar answered Jan 29 '23 11:01

ghstcode