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 🙏

© 2025 – Pkg Stats / Ryan Hefner

fnbind

v1.0.1

Published

A module to construct prototype objects as functions to provide more code readability.

Downloads

4

Readme

fnbind

A module to construct prototype objects as functions to provide more code readability.

Summary


Motivations

The Javascript beauty relies on its flexibility and how easy great things can come with a bunch of simple code. But as the time passes, this "bunch of simple code" can turn into a huge mess because it's now a file of 5000 lines of code and you have no idea of how much methods do you have, which of them are used or not between those functions, methods are now more complex and needs bigger names, and so on.

The way I found to solve this is to doing three things:

  1. Declare all my public methods in alphabetical order on the first place a developer looks when opening a class file;
  2. Use the Javascript flexibility to create objects as functions to have dots separating the words described on a method name;
  3. Declare all my private methods after all public methods implementations.

Well then.. Why?

Code doesn't need to be complicated and neither hard to understand. You can make it way more readable by change this:

ExampleClass.prototype.getListOfFatCatThatSleepsAllDay = function() {
  // ...
};

Into this:

ExampleClass.prototype['get.listOf.fatCats.thatSleepsAllDay'] = function() {
  // ...
};

Because when you need to create a very similar function, but with another purpose, you can instead of doing this:

ExampleClass.prototype.getListOfFatCatThatSleepsAllNight = function() {
  // ...
};

Into this:

ExampleClass.prototype['get.listOf.fatCats.thatSleepsAllNight'] = function() {
  // ...
};

The dots between the words make it way more cleaner for human eyes to read it, and having a big(verbose) function name declaration can make your code way more easier to understand for those who doesn't wrote that code.

Well then, having that in mind, let's see how to use it in an actual Javascript class.

How to use it

Suposing that you are developing a 'nodejs-like' code...

NOTE: There is a javascript class at 'lib/example-class.js' to serve as a full example of use of fnbind, and a test file at 'test/index.test.js' to demonstrate its usage.

  1. Install fnbind with npm and create a javascript file:

    npm install --save fnbind
    touch example-class.js
  2. Declare a common javascript class and exports it:

    const fnbind = require('fnbind');
    
    const ExampleClass = (function() {
      /**
       * @constructor
       */
      function ExampleClass() {
    
      }
    
      return ExampleClass;
    })();
    
    module.exports = ExampleClass;
  3. Inside the constructor, declare all the public methods you want to expose following a javascript plain object structure:

    const fnbind = require('fnbind');
    
    const ExampleClass = (function() {
      /**
       * @constructor
       */
      function ExampleClass() {
        this.get = {
          fatCat: {
            duquesa: fnbind(this['get.fatCat.duquesa'], this),
            solares: fnbind(this['get.fatCat.solares'], this)
          },
          listOf: {
            fatCats: {
              thatSleepsAllDay: fnbind(this['get.listOf.fatCats.thatSleepsAllDay'], this)
            }
          }
        };
      }
    
      return ExampleClass;
    })();
    
    module.exports = ExampleClass;

    NOTE: As a personal preference, I like to declare all public methods on alphabetical order.

  4. Implement your public methods using the Object.prototype:

    const fnbind = require('fnbind');
    
    const ExampleClass = (function() {
      /**
       * @constructor
       */
      function ExampleClass() {
        this.get = {
          fatCat: {
            duquesa: fnbind(this['get.fatCat.duquesa'], this),
            solares: fnbind(this['get.fatCat.solares'], this)
          },
          listOf: {
            fatCats: {
              thatSleepsAllDay: fnbind(this['get.listOf.fatCats.thatSleepsAllDay'], this)
            }
          }
        };
      }
    
      /**
       * @return {fatCat} - A single fatCat instance
       */
      ExampleClass.prototype['get.fatCat.duquesa'] = function() {
        const fatCat = {
          name: 'Duquesa',
          photo: ``,
          reasonWhyImFat: `Bitch please, I'm a duchess. I can be as fat as I want, and I will still be fabulous.`,
          sleepTime: 'day',
          weightInformation: {
            quantity: 17,
            unitOfMeasurement: 'lbs'
          }
        };
    
        return fatCat;
      };
    
      /**
       * @return {fatCat} - A single fatCat instance
       */
      ExampleClass.prototype['get.fatCat.solares'] = function() {
        const fatCat = {
          name: 'Solares',
          photo: '',
          reasonWhyImFat: `I have a very hard routine to maintain my orange and sexy belly:
          I wake up in the morning, eat like a starving beggar, drink tons of water and
          lay on the yard floor pretending that i'm dead.
          When the sun goes down, I go back to my house, eats until I see the food pot bottom, leave it empty so
          my homies stay fit, and go back to my pasteboard castle where I can watch my human slave play video games.`,
          sleepTime: 'day',
          weightInformation: {
            quantity: 22,
            unitOfMeasurement: 'lbs'
          }
        };
    
        return fatCat;
      };
    
      /**
       * @return {Array<fatCat>} - A list of fat cats
       */
      ExampleClass.prototype['get.listOf.fatCats.thatSleepsAllDay'] = function() {
        const fatCat1 = this.get.fatCat.duquesa();
        const fatCat2 = this.get.fatCat.solares();
    
        return [fatCat1, fatCat2];
      };
    
      return ExampleClass;
    })();
    
    module.exports = ExampleClass;
  5. Now you can create a new instance of your class and use it like a normal javascript class:

    touch index.js
    /**
     * index.js
     */
    const ExampleClass = require('./example-class');
    const exampleClass = new ExampleClass();
    
    let amountOfFatCats = exampleClass.get.listOf.fatCats.thatSleepsAllDay().length;
    console.log(amountOfFatCats); // 2

    Done. :)

Running the module tests

npm test