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 🙏

© 2024 – Pkg Stats / Ryan Hefner

custom-class

v1.0.0

Published

Modify the internal behaviour of your JavaScript classes

Downloads

6

Readme

CustomClass

Introduction

CustomClass allows you to customize the internal methods of your JavaScript classes, in the same way that you might in other languages like Python or Ruby.

For example, you might want an object that lets you call it like a function:

class MyClass extends CustomClass {
    __apply__(){
        return "I'm a function!";
    }
}

const mc = new MyClass();
console.log(mc());
I'm a function!

Or maybe you want an object that has a default value for any key you try to access:

class MyClass extends CustomClass {
    __get__(target, property, receiver, getDefault) {
        if (prop in target) {
            return getDefault();
        }
        else {
            return "DEFAULT VALUE";
        }
    }
}

const mc = new MyClass();
console.log(mc.foo);
console.log(mc.bar);
DEFAULT VALUE
DEFAULT VALUE

Installation

Run:

npm install custom-class --save

Then import the class:

const CustomClass = require('custom-class');

Usage

Simply inherit from the CustomClass, and implement any of the following double-underscore methods. In general, the signature of these methods matches the corresponding methods on the JavaScript Proxy object, but with an extra argument added on to the end, a function that will return the default value for this method.

Common Arguments

  • target: This object, but without any internal method intercepting. Use this to get information out of your object without fear of causing an infinite loop
  • getDefault: A function that, if called, will perform the default behaviour and return the default value for this method. For example if you override __get__, getDefault() will return the true value of the field the user is trying to access.

List of Methods

(Content based on Proxy handler by Mozilla Contributors, licensed under CC-BY-SA 2.5)

__apply__(target, thisArg, argumentsList, getDefault)

Invoked when an instance of this class is called as a function

e.g. myInstance()

  • thisArg: The this argument for the call
  • argumentsList: The list of arguments for the call

__construct__(target, argumentsList, newTarget, getDefault)

Invoked when an instance of this class is used as a constructor

e.g. new myInstance()

  • argumentsList: The list of arguments for the constructor
  • newTarget: The object that is being constructed

__defineProperty__(target, property, descriptor, getDefault)

Invoked when an instance of this class has Object.defineProperty() called on it

  • property: The name of the property whose description is to be retrieved
  • descriptor: The descriptor for the property being defined or modified

__deleteProperty__(target, property, getDefault)

Invoked when an instance of this class has one of its fields deleted

e.g. delete myInstance.foo

  • property: The name of the property to delete

__get__(target, property, receiver, getDefault)

Invoked when an instance of this class has one of its fields accessed

e.g. myInstance.foo

  • property: The name of the property to get
  • receiver: Either the proxy or an object that inherits from the proxy

__getOwnPropertyDescriptor__(target, prop, getDefault)

Invoked when an instance of this class has Object.getOwnPropertyDescriptor() called on it

  • prop: The name of the property whose description should be retrieved

__getPrototypeOf__(target, getDefault)

Invoked when an instance of this class has its prototype checked,

e.g. Object.getPrototypeOf(myInstance), or myInstance instanceof SomeClass

__has__(target, property, getDefault)

Invoked when an instance of this class has the in operator applied to it

e.g. "foo" in myInstance

  • property: The name of the property to check for existence.

__isExtensible__(target, getDefault)

Invoked when an instance of this class has Object.isExtensible() called on it

__ownKeys__(target, getDefault)

Invoked when an instance of this class has Object.getOwnPropertyNames() called on it

__preventExtensions__(target, getDefault)

Invoked when an instance of this class has Object.preventExtensions() called on it.

__set__(target, property, value, receiver, getDefault)

Invoked when an instance of this class has one of its fields set

e.g. myInstance.foo = "bar"

  • property: The name of the property to set
  • value: The new value of the property to set
  • The object to which the assignment was originally directed

__setPrototypeOf__(target, prototype)

Invoked when an instance of this class has its prototype set

  • prototype: The object's new prototype or null using Object.setPrototypeOf()

Example: Making a defaultdict

In this example, you want to make a JavaScript equivalent of Python's defaultdict: a dictionary that has a default value for all keys you haven't set yourself:

class DefaultDict extends CustomClass {
    constructor(defaultConstructor) {
        super();
        this.defaultConstructor = defaultConstructor;
    }

    __get__(target, prop, receiver, getDefault) {
        if (prop in target) {
            // If we already have a value for this, return it
            return getDefault();
        }
        else {
            // If we don't, generate a default value using our default constructor, and save it onto the object
            const generated = new this.defaultConstructor();
            target[prop] = generated;
            return generated;
        }
    }
}

const dd = new DefaultDict(Array);

assert.deepEqual(dd.foo, []);

dd.bar.push('a');
assert.deepEqual(dd.bar, ['a'])