New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

abstract-class

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abstract-class

Prevents instantiation of a parent class. Optionally defines properties that must be implemented by child classes.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

abstract-class

Prevents instantiation of a parent class. Optionally defines properties that must be implemented by child classes. Imitates the functionality provided by abstract classes in languages like Java, PHP, and C++.

Installation

Requires Node.js 6.0.0 or above.

npm i abstract-class

API

The module exports a single function. This function should be called in the constructor of the class you want to declare abstract.

Parameters

  1. Bindable: cls (Function): The abstract parent class.
  2. obj (Object): The this variable of your object being constructed.
  3. Optional: ...props (any number of: string, number, symbol, or Array thereof): The keys of the properties that a child class should implement.

Return Value

The function does not return a value.

Example

const enforceAbstractClass = require('abstract-class')

class Parent {
  constructor () {
    enforceAbstractClass(Parent, this, 'title', 'content')
  }
}

class Child extends Parent {
  get title () { return 'Title' }
}

// Throws Error: Cannot instantiate abstract class `Parent`
const parent = new Parent()

// Throws Error: `Child` must define the abstract property `content`
const child = new Child()

Constructing a Parent object fails because the class is marked abstract. Constructing a Child object also fails because Child does not define content.

You can also use the bind operator pattern:

const Abstract = require('abstract-class')

class Parent {
  constructor () {
    Parent::Abstract(this)
  }
}

// Throws Error: Cannot instantiate abstract class `Parent`
const parent = new Parent()

Limitation

If the child class provides its own constructor and fails to call the parent constructor, property abstraction will not be enforced.

Keywords

FAQs

Package last updated on 04 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc