Class Utils

0.3.6 · maintenance · verified Tue Apr 21

class-utils is a JavaScript utility library designed to assist developers in working with and manipulating JavaScript classes and their prototype methods. As of its current stable version, 0.3.6, it provides functions for tasks such as checking for property existence (`has`, `hasAll`), type coercion (`arrayify`), inspecting constructors (`hasConstructor`), retrieving property descriptors (`getDescriptor`), and managing inheritance and property copying between objects (`copyDescriptor`, `copy`, `inherit`). The library's focus is on lower-level prototype manipulation, which was more common in pre-ES6 JavaScript class patterns, though its utilities remain functional for modern class syntax when deeper introspection or direct prototype modification is required. It typically follows a stable, low-cadence release cycle given its foundational utility nature. A key differentiator is its explicit focus on descriptors and direct prototype manipulation, offering fine-grained control over object properties and inheritance chains beyond typical `Object.assign` or spread operator usage. It is primarily a CommonJS module built for Node.js environments from version 0.10.0 and up.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing the class-utils library and using `copyDescriptor` to transfer a property descriptor from one object's prototype to another.

const cu = require('class-utils');

function App() {}
Object.defineProperty(App.prototype, 'count', {
  get: function() {
    return Object.keys(this).length;
  }
});

const obj = {};
cu.copyDescriptor(obj, App.prototype, 'count');

console.log(Object.getOwnPropertyDescriptor(obj, 'count'));
// Expected output: { get: [Function], set: undefined, enumerable: false, configurable: false }

view raw JSON →