npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lopatnov/callable

v3.1.0

Published

Make TypeScript/JavaScript class instances callable as functions. Four implementation strategies: bind, closure, proxy, and callee.

Readme

@lopatnov/callable

A TypeScript abstract base class that lets you create class instances that behave as callable functions. Extend Callable<TResult>, implement _call, and every new instance becomes directly invokable — with full prototype chain, type safety, and IDE completion preserved.

npm downloads npm version License GitHub issues GitHub stars


Table of Contents


Installation

npm install @lopatnov/callable

Browser (CDN via jsDelivr):

<script src="https://cdn.jsdelivr.net/npm/@lopatnov/callable/dist/byBind.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lopatnov/callable/dist/byCallee.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lopatnov/callable/dist/byClosure.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lopatnov/callable/dist/byProxy.umd.min.js"></script>

Usage

TypeScript / ES Modules

import CallableByBind from "@lopatnov/callable/byBind";
import CallableByCallee from "@lopatnov/callable/byCallee";
import CallableByClosure from "@lopatnov/callable/byClosure";
import CallableByProxy from "@lopatnov/callable/byProxy";

class Greeter extends CallableByBind<string> {
  _call(...args: any[]): string {
    return `Hello, ${args[0]}!`;
  }
}

const greet = new Greeter(); // instance is a callable function
console.log(greet("World")); // "Hello, World!"

CommonJS

const CallableByBind = require("@lopatnov/callable/byBind");

class Greeter extends CallableByBind {
  _call(...args) {
    return `Hello, ${args[0]}!`;
  }
}

const greet = new Greeter();
console.log(greet("World")); // "Hello, World!"

Browser UMD

After loading the script tag, the class is available as the global callable:

<script src="https://cdn.jsdelivr.net/npm/@lopatnov/callable/dist/byBind.umd.min.js"></script>
<script>
  class Greeter extends callable {
    _call(...args) {
      return `Hello, ${args[0]}!`;
    }
  }

  const greet = new Greeter();
  console.log(greet("World")); // "Hello, World!"
</script>

API

Abstract method

Every subclass must implement:

abstract _call(...args: any[]): TResult

| Parameter | Type | Description | | --------- | ------- | ---------------------------------------------------------- | | ...args | any[] | Arguments passed when the instance is called as a function |

Returns: TResult — the value returned to the caller.

Implementations comparison

| Class | Import path | Mechanism | Strict mode | Modifies prototype | | ------------------- | ------------------------------ | --------------------------------- | ------------------------ | ------------------ | | CallableByBind | @lopatnov/callable/byBind | Function.prototype.bind | ✅ Yes | ❌ No | | CallableByCallee | @lopatnov/callable/byCallee | arguments.callee | ❌ No (sloppy mode only) | ❌ No | | CallableByClosure | @lopatnov/callable/byClosure | Closure + Object.setPrototypeOf | ✅ Yes | ✅ Yes | | CallableByProxy | @lopatnov/callable/byProxy | Proxy apply trap | ✅ Yes | ❌ No |


CallableByBind<TResult>

Uses Function.prototype.bind to return a bound function from the constructor. The bound function delegates to _call on the original instance.

Pros: no deprecated APIs, no prototype modification, works in strict mode.

Cons: constructor returns a bound wrapper, not the raw instance.

import CallableByBind from "@lopatnov/callable/byBind";

class Multiplier extends CallableByBind<number> {
  constructor(private factor: number) {
    super();
  }
  _call(value: number): number {
    return value * this.factor;
  }
}

const triple = new Multiplier(3);
console.log(triple(7)); // 21

CallableByCallee<TResult>

Uses arguments.callee inside the dynamically constructed function body.

Pros: minimal implementation.

Cons: arguments.callee is forbidden in strict mode — not suitable for modern bundlers.

const CallableByCallee = require("@lopatnov/callable/byCallee");

class Adder extends CallableByCallee {
  _call(a, b) {
    return a + b;
  }
}

const add = new Adder();
console.log(add(2, 3)); // 5

CallableByClosure<TResult>

Creates an anonymous function in the constructor that closes over itself, then rewires the prototype chain with Object.setPrototypeOf.

Pros: works in strict mode, no bound wrapper.

Cons: calls Object.setPrototypeOf, which may affect JIT optimization.

import CallableByClosure from "@lopatnov/callable/byClosure";

class Counter extends CallableByClosure<void> {
  private count = 0;
  _call(): void {
    console.log(++this.count);
  }
}

const counter = new Counter();
counter(); // 1
counter(); // 2

CallableByProxy<TResult>

Wraps the constructed instance in an ES6 Proxy with an apply trap that delegates to _call.

Pros: clean ES2015+ approach, works in strict mode, no prototype modification.

Cons: adds a Proxy wrapper with a minor per-call overhead.

import CallableByProxy from "@lopatnov/callable/byProxy";

class Logger extends CallableByProxy<void> {
  _call(message: string): void {
    console.log(`[LOG] ${message}`);
  }
}

const log = new Logger();
log("Server started"); // [LOG] Server started

Distribution Formats

Each implementation is published in four formats:

| Format | File | Use case | | -------------- | ------------------------ | -------------------------------- | | CommonJS | dist/{name}.cjs | Node.js require() | | ES Module | dist/{name}.esm.mjs | Bundlers, native ESM import | | UMD | dist/{name}.umd.js | Browser globals (with sourcemap) | | UMD (minified) | dist/{name}.umd.min.js | Production browser |

TypeScript declaration files (*.d.ts) are included for all four implementations.


Demo

Try it live:


Contributing

Contributions are welcome! Please read CONTRIBUTING.md before opening a pull request.


Built With

  • TypeScript — strict typing throughout
  • Rollup — bundled to ESM, CJS, and UMD formats
  • Ava — fast, concurrent test runner
  • OXLint — fast JavaScript/TypeScript linter
  • dprint — code formatter

License

Apache-2.0 © 2019–2026 Oleksandr Lopatnov · LinkedIn