Class Utils
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
-
TypeError: class_utils_1.default is not a function
cause Attempting to import `class-utils` using an ES Module `import` statement in an environment that expects CommonJS exports as a default.fixChange your import statement to `const cu = require('class-utils');` -
TypeError: cu.someMethod is not a function
cause The imported `cu` object does not have the specified method, or `class-utils` was not correctly imported, resulting in `cu` being undefined or an unexpected value.fixVerify the method name is correct according to the API (`cu.has`, `cu.copyDescriptor`, etc.) and ensure `const cu = require('class-utils');` executed without error.
Warnings
- gotcha This package is a CommonJS module and does not provide native ES Module (ESM) exports. Attempting to use `import` statements directly in an ESM context will likely result in errors unless a bundler or transpiler (like Webpack or Babel) is configured to handle CJS imports.
- gotcha The package targets older Node.js versions (>=0.10.0), which means its internal implementation may not leverage modern JavaScript features (e.g., native ES6 classes, async/await). While functional, newer syntax or idioms might not be reflected.
- gotcha The utilities primarily interact with JavaScript's prototype chain and property descriptors. Developers unfamiliar with these concepts, especially when primarily working with ES6 `class` syntax, might find the behavior non-intuitive or encounter unexpected results if not used carefully.
Install
-
npm install class-utils -
yarn add class-utils -
pnpm add class-utils
Imports
- cu
import cu from 'class-utils';
const cu = require('class-utils'); - has
import { has } from 'class-utils';const { has } = require('class-utils'); - copyDescriptor
const cu = require('class-utils'); cu.copyDescriptor(targetObj, sourceObj, 'methodName');
Quickstart
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 }