Class.js
Class.js represents my attempt to add semi-classical object-oriented programming capacity to JavaScript without altering the language. What this means is that with Class.js, you can now declare class objects in much the same way you would expect in languages like Java, C#, and C++ (but sorry, no multiple inheritance).
Class.js comes in 2 flavors: ES5 & ES6. The ES6 implementation brings new syntax and is not yet as flexible as the older ES5 version. However, it is a much lighter implementation (1/10 the code). The features and syntax support of the ES5 version will be added to the ES6 version as time permits.
ES5 Example:
var Class = ; //The class declarator itself.Class; //Declare "class SampleBase {}"var SampleBase = "SampleBase" //Private Static Data counter: //Private Data className: ; //Private Method logCounter: //Protected Property Counter: ; //Declare descendant class Sample as a singletonvar Sample = "Sample" Extends: SampleBase //Private Static Data instance: //Instance Constructor Constructor: //Public Static Method getInstance: //Public Instance Method getInstanceCount: ; var sample = Sample;console;
ES6 Example (same logic):
var Class = ; //The class declarator itself. //Declare "class SampleBase {}"var SampleBase = ; //Declare descendant class Sample as a singletonvar Sample = instance: null //private + static isn't yet supported. /* This isn't yet supported constructor() { super(); console.log("Created Sample instance"); } */ { super; } static { //'this' inside a static function refers to the //class static scope. if !thisinstance /* Internal constructor method ```Instance``` isn't supported yet. this[instance] = new this[Instance](); */ thisinstance = ; ++thisCounter; console; return thisinstance; } )) //Public Instance Method getInstanceCount: ; var sample = Sample;console;
WeakMap.js
To implement Class.js, I needed WeakMap support, which isn't always available. I created a shim to take care of that. Since WeakMaps aren't completely implementable in pure JavaScript, I created a version that leaks memory as little as possible. The result is a WeakMap implementation that only leaks a single GUID string per object stored. Short of this leakage, this WeakMap should be nearly as secure as the native implementation would be. This WeakMap is complete as per the current specification.
Functor.js
I also needed a way of reliably being able to ensure that I could call any class function on its instance. This meant being able to dynamically re-bind function references. Since Function.bind() doesn't allow the re-binding I needed, I implemented this little quickie. There's almost no need to use it directly though. From any class function, "this.Delegate()" will do the work for you. You can pass it the string name of a class member function or a function definition.
From outside a class instance, Functor can be used to ensure a specific object is used when a callback is needed.
Example
var Functor = ; var someObj = count: 7; var method = someObj { console;}; window; //Prints "8" 1 second later
Enum.js
Just because I wanted to be able to constrain a list of values, I created an Enum class as well.
Example
var Enum = ;var WeekDay = "Sunday" "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"; var day = ; //Defaults to "Sunday"dayvalue = 2; //console; //Prints "Tuesday"dayname = "Friday";console; //Prints "5"dayname = "?" //Throws!dayvalue = -1; //Throws!console; //Prints "true"
The enum values can be objects as well. Just declare the Enum as an object instead of an array.
Example
var ObjectEnum = "default" default: foo: null something: {} ;
A Tutorial on this Class.js Library
Since this readme can't really convey enough about how to use Class.js, I'm making a Wiki tutorial to explain the ins and outs of the library.
http://thekingsnotice.blogspot.com/2015/03/oojs-5-re-introducing-classjs.html