1.0.1 • Published 6 years ago
is-class-of v1.0.1
is-class-of
Returns true if A is a subclass of B or the same class as B.
Installation
Requires Node.js 6.0.0 or above.
npm i is-class-of
API
The module exports a single function.
Parameters
- Bindable:
A
(function): What may or may not be B or a subclass of B. B
(function or string): What may or may not be A or an ancestor of A. You can either provide the class itself or its string name.
Return Value
- Returns
true
ifA
is a subclass ofB
, is equal toB
, or has a name equal toB
. - Otherwise
false
.
Example
const isClassOf = require('is-class-of')
isClassOf(Date, Error) // false
isClassOf(Error, Error) // true
isClassOf(Error, 'Error') // true
isClassOf(TypeError, Error) // true
isClassOf(RangeError, Error) // true
isClassOf(RangeError, 'Error') // true
class A {}
class B extends A {}
isClassOf(A, A) // true
isClassOf(A, 'A') // true
isClassOf(B, A) // true
isClassOf(B, 'A') // true
// Supports the bind operator
Error::isClassOf(Error) // true
TypeError::isClassOf(Error) // true
TypeError::isClassOf('Error') // true
B::isClassOf(A) // true
Related
- is-subclass-of: Returns true if A is a subclass of B.
- is-instance-of: Like
instanceof
, but uses class name strings.