{"id":22021557,"url":"https://github.com/lamansky/abstract-class","last_synced_at":"2026-05-05T03:32:53.912Z","repository":{"id":57172120,"uuid":"120179239","full_name":"lamansky/abstract-class","owner":"lamansky","description":"[Node.js] Prevents instantiation of a parent class. Optionally defines properties that must be implemented by child classes.","archived":false,"fork":false,"pushed_at":"2018-02-04T11:35:19.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-23T09:18:03.953Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lamansky.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-04T11:34:16.000Z","updated_at":"2018-02-04T11:35:20.000Z","dependencies_parsed_at":"2022-08-24T13:30:47.735Z","dependency_job_id":null,"html_url":"https://github.com/lamansky/abstract-class","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fabstract-class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fabstract-class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fabstract-class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fabstract-class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lamansky","download_url":"https://codeload.github.com/lamansky/abstract-class/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245085058,"owners_count":20558326,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-30T06:12:55.268Z","updated_at":"2026-05-05T03:32:53.872Z","avatar_url":"https://github.com/lamansky.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# abstract-class\n\nPrevents 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++.\n\n## Installation\n\nRequires [Node.js](https://nodejs.org/) 6.0.0 or above.\n\n```bash\nnpm i abstract-class\n```\n\n## API\n\nThe module exports a single function. This function should be called in the constructor of the class you want to declare abstract.\n\n### Parameters\n\n1. Bindable: `cls` (Function): The abstract parent class.\n2. `obj` (Object): The `this` variable of your object being constructed.\n3. Optional: `...props` (any number of: string, number, symbol, or Array thereof): The keys of the properties that a child class should implement.\n\n### Return Value\n\nThe function does not return a value.\n\n## Example\n\n```javascript\nconst enforceAbstractClass = require('abstract-class')\n\nclass Parent {\n  constructor () {\n    enforceAbstractClass(Parent, this, 'title', 'content')\n  }\n}\n\nclass Child extends Parent {\n  get title () { return 'Title' }\n}\n\n// Throws Error: Cannot instantiate abstract class `Parent`\nconst parent = new Parent()\n\n// Throws Error: `Child` must define the abstract property `content`\nconst child = new Child()\n```\n\nConstructing a `Parent` object fails because the class is marked abstract. Constructing a `Child` object also fails because `Child` does not define `content`.\n\nYou can also use the bind operator pattern:\n\n```javascript\nconst Abstract = require('abstract-class')\n\nclass Parent {\n  constructor () {\n    Parent::Abstract(this)\n  }\n}\n\n// Throws Error: Cannot instantiate abstract class `Parent`\nconst parent = new Parent()\n```\n\n## Limitation\n\nIf the child class provides its own constructor and fails to call the parent constructor, property abstraction will not be enforced.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fabstract-class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamansky%2Fabstract-class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fabstract-class/lists"}