Now that we have native implementations of ES6, there are "real classes". These are largely syntactic sugar for prototypal inheritance as with constructor functions, but there are subtle differences and the two are not completely interchangeable.
So far, the only way I've found is to get the .toString()
of the object's prototype's constructor function and check if it starts with class
OR if the object has a constructor and the .toString()
of that starts with class
.
Note that if your code is compiled (ie: most Babel or TypeScript setups), then this will return function...
instead of class...
at runtime (since classes are transpiled to constructor functions).
function isClass(obj) {
const isCtorClass = obj.constructor
&& obj.constructor.toString().substring(0, 5) === 'class'
if(obj.prototype === undefined) {
return isCtorClass
}
const isPrototypeCtorClass = obj.prototype.constructor
&& obj.prototype.constructor.toString
&& obj.prototype.constructor.toString().substring(0, 5) === 'class'
return isCtorClass || isPrototypeCtorClass
}
This will only work in native environments (Chrome, Firefox, Edge, node.js, etc.) that have implemented class
for code that has not been transpiled to function
.
Usage:
class A {}
class B extends A {}
isClass(A) // true
isClass(new A()) // true
isClass(B) // true
isClass(new B()) // true
function C() {}
isClass(C) // false
isClass(new C()) // false
isClass({}) // false
isClass(Date) // false
isClass(new Date()) // false
//These cases return 'true' but I'm not sure it's desired
isClass(Object.create(A)) // true
const x = {}
Object.setPrototypeOf(x, A)
isClass(x) // true
If there is a better way, I'd love to know what it is.