StrictOOP
Strict Object-Oriented Programming for Javascript
Strict OOP is a way of bringing strict Object-Oriented Programming concepts to Javascript.
StrictOOP has been tested for node.js but should also work in most modern browsers/
Currently it supports:
- Public/Private properties
- Strict typing for class properties
- Public/Private methods
- Strict typing for return values
Strict OOP can be used with ECMA6 classes or ECMA5 OOP via functions.
The features above can all be applied to an object after it's been constructed or inside the object's constructor.
Examples
Private Properties
Properties can be made private so they are inaccessible from methods outside the class.
var StrictOOP = ; //Firstly declare the class as normal { thisspeed = 0; } { thisspeed += 5; } var car = ; //Add strict OOP rules to the `car` objectvar oop = car; //set the speed property to privateoop; //Now, trying to set it will cause an error to be throwncarspeed = 100; //This will update the speed as expected because the accellerate method is inside the classcar;
The example above can also be expressed as:
var StrictOOP = ; //Firstly declare the class as normal { //Add strict OOP rules to this instance var oop = this; oop; thisspeed = 0; } { thisspeed += 5; } //Create an instance of `Car` as normal:var car = ; //But trying to set it will cause an error to be throwncarspeed = 100; //This will update the speed as expected because the accellerate method is inside the classcar;
This approach gives class authors the ability to define whether they want to enforce strict OOP in their classes.
Strict types
StrictOOP also supports any types supported natively by javascript:
var StrictOOP = ; //Firstly declare the class as normal { //Add strict OOP rules to this instance var oop = this; ooptype'number'; thisspeed = 0; } { thisspeed += 5; } //Create an instance of `Car` as normal:var car = ; //Causes an error:carspeed = 'REALLY FAST'; //Allowed:carspeed = 100;
This will thrown an error because the speed
property now only accepts numbers.
Private + Type
Visibility and types can be combined:
var StrictOOP = ; //Firstly declare the class as normal { //Add strict OOP rules to this instance var oop = this; ooptype'number'; thisspeed = 0; } { //This will error due to type checking thisspeed = 'FAST'; } //Create an instance of `Car` as normal:var car = ; //This will errror because of visiblitycarspeed = 100;
Methods
StrictOOP also allows setting visibility and return type checks on methods:
var StrictOOP = ; //Firstly declare the class as normal { //Add strict OOP rules to this instance var oop = this; oopmethod'getSpeed'; oopmethod'accellerate'; thisspeed = 0; } { //This will error due to type checking thisspeed = 'FAST'; } { return 'FAST'; } //Create an instance of `Car` as normal:var car = ; //Errors because it returns a string instead of a numbercar; //Errors because the method is privatecar;
Limitations
Anonymous classes and functions must have names for StrictOOP to work correctly.
Wrong:
var { this { ; }} var MyClass2 = class { }
Right:
var { this { ; }} var MyClass2 = { }