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

js-simple-oop

v1.0.3

Published

Simple OOP inheritance implementation for JavaScript

Downloads

2

Readme

js-simple-oop

Simple OOP inheritance implementation for JavaScript.

It is trying to minimize any conventions and rules on how to write your JS code. So, you will keep writing your JS code as you used to. That includes: defining classes, defining private and public methods, ....

Only one convention (which is originally a JavaScript one) to mention: Add methods in a named way, so try to avoid writing any anonymous/closure method without name. i.e. instead of writing function(param1, param2){ ... } add a name so it will become function someName(param1, param2){ ... } and that will work exactly without any effects on your code and execution.

Install

npm install js-simple-oop

How to use

Include the module

var OOP = require('js-simple-oop');

Creating classes

You can create a class by calling the extendClass method on OOP base class or any extended subclass.


// Creating LivingCreature class as the main class with a constructor
var LivingCreature = OOP.extendClass(function(age){
  this.age = age;
});

// Creating IntelligentCreature extending LivingCreature without a constructor
// so LivingCreature constructor will be used
var IntelligentCreature = LivingCreature.extendClass();

//Creating Human extending IntelligentCreature, and extending the LivingCreature constructor
var Human = IntelligentCreature.extendClass(function(name, age){
  this.parentConstructor(age); //calls parent method
  this.name = name;
});

Extended subclasses will receive the following class-level methods:

  1. .extendClass(subClass) which allows to create subclasses from the current.
  2. .defineMethod(method[, name]) which allows to define new named methods
  3. .defineProperty(property[, getter[, setter[, descriptors]]]) which allows to define a property. Check Mozilla Developer Network for info.

Also, in the base OOP class, you can use following methods:

  1. OOP.extendClass(Class) allows to create new main classes.
  2. OOP.extend(SubClass, MainClass) to inherit MainClass to SubClass.

Adding methods

You can add methods in the normal JavaScript way using the prototype. Please note it is preferred to use the named functions when defining methods.

//this way
LivingCreature.prototype.identify = function identify(){
  //...
};

//is preferred to this way
LivingCreature.prototype.identify = function(){
  //...
};

Or you can use the added .defineMethod method which will exactly the same result

//this way
IntelligentCreature.defineMethod(function identify(){
  console.log('I am an intelligent creature.');
  //One way to call parent overridden methods, thanks to named methods, which made
  //this one possible.
  return this.propagateCall();
});

//is preferred to this way
IntelligentCreature.defineMethod(function(){
  //...
}, 'identify');

Calling overridden methods

In subclasses, you can override an inherited method by define a method with the same name. To call the overridden method from within the overriding one, you can either use the this.parentMethod(methodName[, params...]) or the shorthand this.propagateCall([params...]) which will detect the name of the calling method and call the first method internally.

LivingCreature.defineMethod(function identify(){
  console.log('I am a living creature!');
});

IntelligentCreature.defineMethod(function identify(){
  this.parentMethod('identify');
  console.log('Also, I am an intelligent creature!');
});

Human.defineMethod(function identify(){
  this.propagateCall();
  console.log('And guess what? I am human....');
});

var someone = new Human();
someone.identify();

//result will be:
//I am a living creature!
//Also, I am an intelligent creature!
//And guess what? I am human....

Please note that calling overridden methods this way requires that methods were defined in the named way.

If you are not happy with the named method definitions, you can still use the original JavaScript method calling:

LivingCreature.prototype.idenfity = function(){
  console.log('I am a living creature!');
};

IntelligentCreature.prototype.identify= function(){
  this.__proto__.__proto__.identify();
  console.log('Also, I am an intelligent creature!');
};

If you will go with this traditional approach, please note that the position of the overridden method to be called is relative to the calling object, not calling class.

Creating and using objects

Nothing new here. It is the normal new keyword.

var tree = new LivingCreature();
var dolphin = new IntelligentCreature(12);
var person1 = new Human('Muhammad', 20);
var person2 = new Human('Salim', 47);

Sample

Please check the sample file which includes a nice example.

Author

Muhannad Shelleh [email protected]

License

Published under the MIT license.