Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a function is a class or a function

Tags:

javascript

I'm actually trying to differentiate (in my code) functions and classes.

I need to know, in a simple console.log, if the parameter of my function is a class or a simple function.

I'm actually taking about class, not object, but really a class.

Like:

function(item){
    if(isClass(item)){
        console.log('it s a class !');
    }else{
        if(isFunc(item)){
            console.log('it s a function !');
         }
    }
}

Edit : angular 2 is able to distinguish a function and a class in his injector container class

like image 537
mfrachet Avatar asked Aug 31 '25 10:08

mfrachet


1 Answers

There aren't any classes in JavaScript. Though functions can be instanciated with the new keyword.

You can check if something is a function with instanceof:

var a = function() {};

a instanceof Function; // true

An instanciated function will be an instance of that specific function:

var b = new a();

b instanceof Function; // false
b instanceof a; // true
like image 162
pstenstrm Avatar answered Sep 03 '25 00:09

pstenstrm