Object Oriented JavaScript
OOJ is a JavaScript library designed to combine Object Oriented helpers in JavaScript in a single library. Using OOJ you can create an Enum, Interface and Class by defining objects and calling the necessary functions.
Why OOJ?
Native object creation runs faster, it's possible to handle making classes and doing inheritance with native JavaScript so why would I use this library?
Well, that's a good question. And if native processes work for your needs then by all means - go with those methods. Also consider some similar styled methods like the extend
method from Underscore.js which do a little bit of object creation. If you're using CoffeeScript or TypeScript then you have no need for OOJ, go enjoy your fancy JavaScript.
So, if you've read this far by now you might be even more confused what purpose this library serves. Here's my reasons for creating it (and using it myself). OOJ provides a clean, clear and somewhat familiar object oriented method for defining classes. It also emulates Interfaces and Enums to provide some additional functionality aside from simple classes. Creating a class, and extend classes does run slower than native JavaScript, but only whent the script is loaded - which isn't that much of an impact on overall performance.
Aside from the syntax it provides some other OO features that are harder to simulate without some work such as the ability to extend and implement previously defined classes. A proper isInstanceOf
method (and for classes isAssignableFrom
) which makes inheritance useful. The ability to call super methods from an overridden child method via this.$super
and of course object creation chains to extended classes.
So, Why should you choose OOJ? Well, because it's a simple API to define classes that can extend and implement in a more true to life OO method that you might be familiar with! It makes large object heavy projects much simpler!
Just check out these examples of OOJ's features.
Creating Enum Objects
In order to create an Enum object you must define a values
property as an array of names or name-value pairs.
Defining a simple Enum is... well simple!
var Gender = ooj; // From here, you can use the Enumvar user = gender: GenderMale;if usergender === GenderMale console;
By default, each name is assigned a value starting at 0 unless values are specified. Specify values by passing an array as the member in the values key.
var Gender = ooj; // From here, you can use the Enumif GenderMalevalue === 10 console;
Values can be set to start a sequence, and if the value is a number it will continue from that point, but values can be set to any type necessary - although the last number in the numeric sequence will be used for all enumerated values without an explicit value.
var Sample = ooj; if SampleSixvalue === 6 console;
You can define functions on enum objects, allowing for more useful and dynamic enum objects. After all, if this wasn't the case then an enum would be nothing more than a plain JavaScript object.
var Gender = ooj; if GenderMale === "he" && GenderFemale === "she" console;
Creating Interfaces
An Interface made by OOJ is simply a JavaScript class with empty functions and a constructor function that throws an error when the Interface is instantiated. Interfaces can be implemented by OOJ Classes. Interfaces are extremely easy to define, assuming you want create an object that has an area function, like a Shape:
var Shape = ooj;
An OOJ Interface is defined by simply specifying the functions that are present on the Interface.
Note: OOJ interfaces, like interfaces from a standard Object Oriented language cannot be instantiated.
var s = ;// InterfacesCannotBeInstantiatedError: Interfaces cannot be instantiated. They must be implemented.
Creating Classes
An OOJ Class isn't much different from a standard JavaScript class but with a simpler and more strightforward declaration syntax. Defining a simple class is just like defining a JavaScript object.
var Simple = ooj; // Use the classvar test = ;test;// "Hello, World"
In order to define a constructor function for OOJ Classes you define a function called construct
on the object given to the call to ooj.Class. There are three other special properties for OOJ Classes, extend
and implement
properties are used to create Classes that receive functionality from other Classes and statics
is an object defining static methods.
The extend
property is used to to extend the functionality of a single class, while the implement
property is used to implement functions from one or more interfaces. The relationships created by these properties can be tested for with a function on each OOJ Class instance, isInstanceOf
.
var Fruit = ooj; var Apple = ooj; var test = ;console;// true
A class can implement one or more interfaces as needed, you can either pass an interface or an array of interfaces via the implement
property such as: implement: Fruit
or implement: [Fruit, Edible]
depending on the needs of the class.
OOJ classes that extend parent classes use a special property on the current instance called $super
that you shouldn't assign or use yourself. This special property will allow you to call your superclass method from the child class method.
var One = ooj;value; // => 1 var Two = ooj;value; // => 2 var Three = ooj;value; // => 3
This magical $super
function is only available in methods that override a parent class method of the same name, and should equal undefined
in all other methods.
OOJ Classes also support static methods (which can be inherited). Unlike instance methods, class methods do not feature the this.$super
parent version to call into so if static functions share functionality then it must be reimplemented (unfortunately :/ I'm searching for a solution to this).
var MyObject = ooj; // Now you can call the static method like thisMyObject; // => 10 // And they can be inherited.var MyOtherObject = ooj; MyOtherObject; // => "one"
All OOJ classes have the ability to fetch the function that constructed the class. This functions similarly to Java's getClass()
or Ruby's #class
methods.
var Fruit = ooj;var f = ;if Fruit === f console; // => "This is true!"