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

nuclei

v0.2.3

Published

Basic classical inheritance system

Downloads

20

Readme

Nuclei

Nuclei provides you with an Object Oriented inheritance system allowing you to straightforwardly create classes, while maintaining standard prototype functionality.

Install

Download the package through npm:

npm install nuclei

Usage

Require Nuclei, the base class from which all others inherit:

var Nuclei = require('nuclei').Nuclei;

Class Properties

Every class has a few properties you can use:

extend method

In order to create a new class, you need to use the extend method, like this:

Nuclei.extend([ClassName], NewClassConstructor, [OptionsObject]);
  • ClassName: A string containing the name of the class you wish to extend. Optional, because each class has its own extend method.

  • NewClassConstructor: A named function containing all the methods for this class. These methods should be assigned to the this variable, and should also be named! This function will be executed immediately and will not be re-executed upon instancing a new object. So anything in here will be shared by all instances.

  • OptionsObject: An optional object containing special configuration. Mostly used by internal Nuclei stuff.

    • also: A class (or array of classes) for multiple inheritance. Especially useful for non-nuclei classes, like EventEmitter.

overload method

This is basically the same as extending the class, but it is used to overwrite the class you're extending.

You can even overload a class that does not exist yet.

Nuclei.overload([ClassName], NewClassConstructor);
  • ClassName: A string containing the name of the class you wish to overload. Optional. If the class does not exist yet, it will be overloaded once it does. Warning: Does not affect already existing instances.

  • NewClassConstructor: Same as the extend method parameter.


parent property

The parent property of the class is a reference to its parent class.


Class Instance Magic Methods

Every instance has a bit of magic:

  • init: This is the 'constructor' method. It will receive all the parameters passed at instancing.

  • __extended__: This method runs when the class is being extended. This runs outside of any instance, but it still has access to 'parent()'.

  • augment: This method creates an 'augmented' version of the current instance. You can read more about this in the 'augmentation' section.

  • augmented: This method runs after the instance has been augmented.

  • parent: The parent method calls a method from the parent class. More can be read in the 'parent' section.


Create a new class

You can create a basic class by using the extend method in this way:

var Animal = Nuclei.extend(function Animal() {

	this.init = function init() {
		console.log('Animal has been inited');
	};

	this.speak = function speak() {
		console.log('What does a generic animal say?');
	};
});

var myAnimal = new Animal();
// >>> "Animal has been inited"

myAnimal.speak();
// >>> "What does a generic animal say?"

Extend a class

This is basically the same as creating a new class, since creating a new class is actually extending from the Nuclei base class.

var Dog = Animal.extend(function Dog() {

	this.init = function init(color) {
		this.color = color;
		console.log('Dog has been inited');
		this.parent();
	};

	this.speak = function speak() {
		console.log('The ' + this.color + ' dog says: woef!');
	};
});

var myDog = new Dog();
// >>> "Dog has been inited"
// >>> "Animal has been inited"

myDog.speak();
// >>> "The brown dog says: woef!"

Augmenting an instance

In certain cases it could be wasteful to create a new instance of a class. Instead you can augment the instance, which basically creates a new context for the instance and uses that.

var myAugmentedDog = myDog.augment({color: 'augmented'});
myAugmentedDog.speak();
// >>> "The augmented dog says: woef!"

Parent

You can call a parent method in several ways inside a method:

  • this.parent() calls the parent of the current method, passing the same arguments
  • this.parent(name) calls the name method of the parent class, passing the same arguments
  • this.parent(name, [arg1, arg2]) calls the name method of the parent class, passing the arguments inside the array as parameters
  • this.parent(name, null, arg1, arg2, arg3) calls the name method of the parent class, passing the extra arguments as parameters

Prototype

The prototype system can still be used to add methods to already existing classes. You can also use the parent method inside it.

All children classes will also be able to access this new method, even after instancing.

Animal.prototype.newlyAddedMethod = function newlyAddedMethod(){
	console.log('AnimalMethod');
	this.parent();
};