Basic classical inheritance system
Nuclei provides you with an Object Oriented inheritance system allowing you to straightforwardly create classes, while maintaining standard prototype functionality.
Download the package through npm:
npm install nuclei
Nuclei, the base class from which all others inherit:
var Nuclei = require'nuclei'Nuclei;
Every class has a few properties you can use:
In order to create a new class, you need to use the extend method, like this:
NucleiextendClassName NewClassConstructor OptionsObject;
ClassName: A string containing the name of the class you wish to extend.
Optional, because each class has its own
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
OptionsObject: An optional object containing special configuration. Mostly used by internal Nuclei stuff.
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.
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.
The parent property of the class is a reference to its parent class.
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.
You can create a basic class by using the
extend method in this way:
var Animal = Nucleiextendthisinit =console.log'Animal has been inited';;thisspeak =console.log'What does a generic animal say?';;;var myAnimal = ;// >>> "Animal has been inited"myAnimalspeak;// >>> "What does a generic animal say?"
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 = Animalextendthisinit =thiscolor = color;console.log'Dog has been inited';thisparent;;thisspeak =console.log'The ' + thiscolor + ' dog says: woef!';;;var myDog = ;// >>> "Dog has been inited"// >>> "Animal has been inited"myDogspeak;// >>> "The brown dog says: woef!"
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 = myDogaugmentcolor: 'augmented';myAugmentedDogspeak;// >>> "The augmented dog says: woef!"
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
namemethod of the parent class, passing the same arguments
this.parent(name, [arg1, arg2])calls the
namemethod of the parent class, passing the arguments inside the array as parameters
this.parent(name, null, arg1, arg2, arg3)calls the
namemethod of the parent class, passing the extra arguments as parameters
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.