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

prototype-extension

v0.2.2

Published

Extension method in javascript without polluting prototypes

Downloads

14

Readme

prototype-extension 0.2.2

###Extension methods brought to javascript

##How does it work ? ###I. Create an extension

// ./string-extension.js

module.exports = class StringExtension
{
    static toInt(self)
    {
        return +self
    }
}

###II. Extend a type with your extension class

// ./main.js

const PrototypeExtension = require("prototype-extension")
const StringExtension = require("./string-extension")

String._.extendWith(StringExtension)
// This can work too:
// ""._.extendWith(StringExtension)

It will allow you to use the StringExtension methods on every string of your module. It won't affect the other modules, neither the ones which import yours.

###III. Use your extension method

// ./main.js

/*
 * Previous code
 */

console.log(typeof "42"._.toInt()) // => number

##Choose your accessor name Go into your package.json and add this section:

{
    /* Your package.json stuff */
    "prototype-extension": {
        "accessor": "$$"
    },
    /* Your other package.json stuff */
}

Now, the default accessor will be "$$" in your module.

String.$$.extendWith(StringExtension)
"123".$$.toInt()

It's useful to avoid conflicts with lodash and underscore.

##See which extensions are accessibles

""._.__extensions__()
// => {
//    StringExtension: Function StringExtension,
//    PrototypeExtension: Function PrototypeExtension
// }

This will show every extensions available for this type (even the inherited ones)

To have more information, you call it like this:

""._.__extensions__(true) // __extensions__(self, complete=false)
// => {
//    _: {
//      String: { StringExtension: Function StringExtension },
//      Object: { PrototypeExtension: Function PrototypeExtension }
//    }
// }

It will indicate how to access the extensions, on which type the extension came from and all the extensions for the types of the prototype chain.

##See which extension methods are accessibles

""._.__extensionmethods__()
// => {
//    toInt: Function toInt
//    extendWith: Function extendWith
//    __extensions__: Function __extensions__
//    __extensionmethods__: Function __extensionmethods__
//    __protochain__: Function __protochain__
//    __protoproperties__: Function __protoproperties__
// }

This will show every extension methods available for this type (even the inherited ones)

To have more information, you call it like this:

""._.__extensionmethods__(true)
// => {
//    _: {
//      String: {
//          StringExtension: {
//              toInt: Function toInt
//          }
//      },
//      Object: {
//          PrototypeExtension: {
//              extendWith: Function extendWith
//              __extensions__: Function __extensions__
//              __extensionmethods__: Function __extensionmethods__
//              __protochain__: Function __protochain__
//              __protoproperties__: Function __protoproperties__
//          }
//      }
//    }
// }

##Unextend your prototype

String._.unextendWith(StringExtension)

""._.toInt => Will throw an error
""._.__extensions__().StringExtension // undefined
""._.__extensionmethods__().toInt // undefined

It will affect your whole module. Every strings won't be able to use the StringExtensions methods. However, it won't affect the node_modules (either the ones you use or the ones that use yours)

#You can't...

##see the extensions added in a node_module and a node_module can't see your extensions

You can't add methods dynamically on your extension after extending your prototype.

String._.extendWith(StringExtension)
StringExtension.dynamicMethod = function (self) { return "dynamicMethod" }
""._.dynamicMethod() // => Will throw an error

##add twice the same extension on the same prototype

String._.extendWith(StringExtension)
String._.extendWith(StringExtension) // Will throw an error

But this will work

class A {}
class B extends A {}

class Extension {}
A._.extendWith(Extension)
B._.extendWith(Extension)

##add two extension with the same method name on the same prototype

class A {}

class ExtensionHello {
    static method(self) {
        console.log('Hello')
    }
}

class ExtensionWorld {
    static method(self) {
        console.log('World')
    }
}

A._.extendWith(ExtensionHello)
A._.extendWith(ExtensionWorld) // Will throw an error

###But this

class A {}
class B extends A {}

class ExtensionHello {
    static method(self) {
        console.log('Hello')
    }
}

class ExtensionWorld {
    static method(self) {
        console.log('World')
    }
}

A._.extendWith(ExtensionHello)
B._.extendWith(ExtensionWorld) // Will work

new A()._.method() // => 'Hello'
new B()._.method() // => 'World'

#Be careful

const obj = {}
obj._ // or obj.yourAccessor will always be true

// So you can't expect this to work as usual
obj._ || (obj._ = "value")

###but

const obj = {}
obj.hasOwnProperty("_") // => false

obj._ = "Hello"
obj.hasOwnProperty("_") // => true

##Threading No tests were made for testing this. If you use a library for threading, you should process with caution. It should work, but there is no guarantee.