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 = 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;
-
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;
-
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; var myAnimal = ;// >>> "Animal has been inited" myAnimal;// >>> "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; var myDog = ;// >>> "Dog has been inited"// >>> "Animal has been inited" myDog;// >>> "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;myAugmentedDog;// >>> "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 argumentsthis.parent(name)
calls thename
method of the parent class, passing the same argumentsthis.parent(name, [arg1, arg2])
calls thename
method of the parent class, passing the arguments inside the array as parametersthis.parent(name, null, arg1, arg2, arg3)
calls thename
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.
Animalprototype{ console; thisparent;};