Backbone-like Class Extension Utility
The `class-extend` package provides a utility inspired by Backbone.js's `.extend` method, enabling classical inheritance for JavaScript objects, primarily targeting Node.js environments. It allows developers to create child classes that inherit prototype and static methods from a parent, with an optional constructor definition. Designed before native ES6 `class` syntax was widely adopted, it offers a programmatic way to manage inheritance chains, including access to a `__super__` property for parent prototype referencing. The latest version officially published on npm is `0.1.2`, last updated in 2015. Given its age and the prevalence of native ES6 `class extends` syntax, the package is no longer actively maintained and has a very slow release cadence (effectively none since its last update). Its key differentiator was providing a structured inheritance mechanism in a CommonJS context when native alternatives were less accessible or standardized.
Common errors
-
TypeError: require is not a function
cause Attempting to use `require('class-extend')` in an ES module context.fixThis package is CommonJS-only. If your project is an ES module, you must transpile CommonJS modules (e.g., with Webpack or Rollup) or rewrite the import to dynamic `import('class-extend')` (though `class-extend`'s direct usage patterns might still conflict with modern ES module expectations). -
ReferenceError: Base is not defined
cause Forgetting to assign the result of `require('class-extend')` to a variable, or attempting to use `Base.extend` without first defining `Base`.fixEnsure you have `const Base = require('class-extend');` at the top of your file before attempting to use `Base` or its `extend` method. -
Property 'extend' does not exist on type 'MyClass'.
cause When trying to add the `.extend` helper to an existing TypeScript class, TypeScript's type inference does not automatically know about the dynamically added property.fixDeclare the `extend` property on your class interface or use a type assertion: `(MyClass as any).extend = require('class-extend').extend;` for JavaScript or define an interface if migrating to TypeScript.
Warnings
- breaking Prior to v0.1.1, a bug caused `child.constructor` to incorrectly point to the parent class's constructor. This was fixed in v0.1.1, meaning code relying on the incorrect behavior would break.
- gotcha This package implements a pre-ES6 class inheritance pattern using CommonJS `require()`. Modern JavaScript development typically uses native `class extends` syntax and ES Modules for inheritance, which provides better type checking with TypeScript and more idiomatic module loading. Relying on `class-extend` might complicate integration with modern toolchains.
- gotcha The `class-extend` package is effectively abandoned, with its last release over 9 years ago (as of 2025). There will be no further updates, bug fixes, or security patches, which poses a risk for long-term project stability and maintenance.
Install
-
npm install class-extend -
yarn add class-extend -
pnpm add class-extend
Imports
- Base
import Base from 'class-extend';
const Base = require('class-extend'); - extend
import { extend } from 'class-extend';const extend = require('class-extend').extend;
Quickstart
const Base = require('class-extend');
// 1. Extend from the blank Base class
const Animal = Base.extend({
constructor(name) {
this.name = name;
},
sayName() {
console.log(`My name is ${this.name}.`);
},
staticMethod() {
console.log('This is a static method from Animal.');
}
}, {
// Static properties/methods
type: 'Mammal'
});
const Dog = Animal.extend({
constructor(name, breed) {
Animal.prototype.constructor.call(this, name); // Call parent constructor
this.breed = breed;
},
bark() {
console.log('Woof!');
},
describe() {
console.log(`I am a ${this.breed} named ${this.name}.`);
}
});
const myDog = new Dog('Buddy', 'Golden Retriever');
myDog.sayName();
myDog.bark();
myDog.describe();
console.log(`Animal type: ${Animal.type}`);
Dog.__super__.staticMethod();