1.0.1 • Published 6 years ago

is-subclass-of v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

is-subclass-of

Returns true if A is a subclass of B.

Installation

Requires Node.js 6.0.0 or above.

npm i is-subclass-of

API

The module exports a single function.

Parameters

  1. Bindable: A (function): What may or may not be a subclass of B.
  2. B (function or string): What may or may not be an ancestor of A. You can either provide the class itself or its string name.

Return Value

  • Returns true if A is a subclass of B.
  • Otherwise false.

Example

const isSubclassOf = require('is-subclass-of')

isSubclassOf(Date, Error) // false
isSubclassOf(Error, Error) // false

isSubclassOf(TypeError, Error) // true
isSubclassOf(RangeError, Error) // true
isSubclassOf(RangeError, 'Error') // true

class A {}
class B extends A {}
isSubclassOf(A, A) // false
isSubclassOf(B, A) // true
isSubclassOf(B, 'A') // true

// Supports the bind operator
TypeError::isSubclassOf(Error) // true
TypeError::isSubclassOf('Error') // true
B::isSubclassOf(A) // true

Related

  • is-class-of: Returns true if A is a subclass of B or the same class as B.
  • is-instance-of: Like instanceof, but uses class name strings.