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

newclass

v0.1.0

Published

Create php-like classes and extend them (supports private, protected and public visibility).

Readme

newClass.js

Create php-like classes and extend them (supports private, protected and public visibility).

###Beware This document differs between classes and objects. Classes are created with var MyClass = newClass({...}), objects are created with new MyClass(), so the class is actually only the blueprint for the object.

Why another oop-class-thingy for javascript?

Because i like the way how php (and most other languages) handles the visibility of properties and methods, so you can decide what the extending class shall be able to access. I searched for other solutions but did not find any good ones, they were either too complicated (in my eyes) or too concentrated on using the normal prototype way (so they were actually just a workaround or an abstraction).

I tried to be as close to the javascript-style as possible, but still provide an easy way to create classes that can be extended with ease.

Can you give me an example?

There are a couple of examples in the examples-dir that show you how things are done, but sure, here is a simple class:

var MyClass = newClass({
    "private": {
        "iAmPrivate": "Secret things no other class should know"
    },
    "protected": {
        "number": null
    },
    "public": {
        "__": function(number) { //"__construct" is also possible
            this.protected.number = number;
        },
        "getNumber": function() {
            return this.protected.number;
        },
        "showNumber": function() {
            console.log(this.public.getNumber());
        }
    }
});

var myClass = new MyClass(13);
myClass.showNumber(); //13

You don't need to declare all visibilities, every single class-option is optional. So a class can actually be also an empty object:

var AnotherClass = newClass({});

Features:

  • Declare properties and methods with special visibities (private, protected and public)
  • Extend classes and override protected and public properties / methods with ease, or add new ones
  • Access the extended class via the this.parent-object (like in php)

TODO

  • implement static properties and methods
  • implement abstract classes and methods
  • implement interfaces
  • an option for something like an "instanceof"-Test (or even make the "instanceof"-statement usable by the classes and objects created with newClass)

FAQ

####The class-options are saved as a public property of the prototype-object, why do you still call it private? That's right, with some hacking around you can actually even change the private properties and methods of the class you want to extend (before you extend her), but hacking is always possible. For example you could also read the text file and parse the data out of that (and then create a new class with the changed data). ####Does extending work over multiple files (with require) or even over multiple modules? Yes, both works because the class-options are saved as a public property in the prototype of the class-object (the thing newClass() returns). ####Aren't private, protected, public, extends and so one reserved words in javascript? Yeah, they are, but as of ECMASCript 5, reserved words are allowed as properties of objects and can be accessed with and without scare-brackets and quotes. So this.protected.myVariable is completely fine. :)

License

This software is licensed under the Mozilla Public License v. 2.0. For more information, read the file LICENSE.