Now that classes are in ES6+, I'd like to be able to check if a value is a regular function or a class definition, something like:
console.log(isClass(class A {}), 'should be true')
console.log(isClass(class B {}), 'should be true')
console.log(isClass(function () {}), 'should be false')
However, I can't seem to find any ways of figuring this out. I've thought of using Object.getOwnPropertyNames(class A {})
as well as (class A {}).__proto__ === Function.prototype
however they return true
for functions as well.
So far the best I've come up with is:
function isClass (thing) {
return typeof thing === 'function' && Object.getOwnPropertyNames(thing).indexOf('arguments') === -1
}
But that doesn't seem that robust.