Backbone-like Class Extension Utility

1.0.0 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates extending from the base class and applying the `extend` helper to create an inheritance chain, including constructor calls and static methods.

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();

view raw JSON →