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

class-bind

v0.2.0

Published

bind-on-access for class methods, using decorators or a simple function

Downloads

372

Readme

class-bind is a utility to make certain methods of a class be bound on access.

It's careful to preserve things like C.prototype.foo giving the original function and the enumerable/writable/configurable flags.

Why?

The main motivation was meta-programming in development. If a method on the prototype is assigned to, it will also be autobound and instances will receive it on the next access. This is disabled in production for performance reasons.

Also I couldn't find anything that let me use decorators to say what should be bound.

Example

import {bound, bind, bindAll} from 'class-bind';

class A {
    // bound with a decorator
    @bound
    foo(){ return this.x; }

    bar(){ return this.y; }
}


// bind a single method
bind(A.prototype, 'bar');

// or bind every method
bindAll(A.prototype);

Install

npm install --save class-bind

Docs

bound

bound is intended to be used as a decorator. Look at the [src/bind.js][src/bind.js] for an example of how it'd work without a decorator.

bind

Takes a protoype and a key, and replaces it with a modified descriptor. It's just bound + Object.defineProperty.

bindAll

Takes a prototype, binds all own property methods. It's just Object.getOwnPropertyNames + bind.

Usage

process.env.NODE_ENV

If this is running in node or browserify: cool. If it's webpack, you need to use DefinePlugin to provide it.

If you don't do this correctly you'll get an error, e.g. in v8:

ReferenceError: process is not defined
TypeError: Cannot read property 'NODE_ENV' of undefined

Paths

require('class-bind') would give you an object with the three functions above. It resolves to class-bind/lib/index.js', which is es5.

Alternatively, you can e.g. require('class-bind/lib/bound') to just get a single function.

The source is es6 and can be found in class-bind/src.

Development

The ususal npm stuff:

# prepare environment
npm install

# run tests
npm test

# es6 in src -> es5 in lib
npm run prepublish