###Professional Object Oriented Framework for web JavaScript and Node.js### POOF.js is a step towards higher quality, faster, cleaner and more maintainable JavaScript object-oriented code.
===
POOF.js is not a new language. It does not require compilation. It is a JavaScript framework providing advanced, powerful and highly efficient features known from other object-oriented languages like Java or C#, such as classes, interfaces, inheritance, accessibility specifiers, constants, static fields and many more. It enforces a clean syntax to help keeping the code maintainable even when large team works on it and provides runtime error checking for those who don't obey.
POOF.js is currently in Alpha stage. It has gone through numerous iterations and all its functionality is implemented. It is not recommended to be used on projects with strict deadline yet until it goes Beta, but it is surely high time for all developers to start getting familiar with it.
Official website: www.poofjs.org
Download (dev): poof-dev.min.js
Download (prod): poof.min.js
Maciej Zasada <[email protected]>
===
- Documentation
- Examples
- Defining classes
- Using constructors
- Defining instance fields
- Defining static fields
- Using accessibility specifiers
- Defining constants
- Extending classes
- Overriding methods
- Defining Final classes
- Defining Abstract classes
- Defining Singleton classes
- Defining interfaces
- Implementing interfaces
- Using RequireJS
class$(name, options, definition);
name: string, required
Specifies class name. Should begin with capital letter. Should only contain letters and digits.
options: object, required
Class options. Class type, base class, interfaces.
Structure:
{
type$: int,
extends$: class,
implements$: Array
}
type$
Class type. Use one of:
- class$.PUBLIC: default, public class
- class$.SINGLETON: Singleton class
- class$.ABSTRACT: abstract class
- class$.FINAL: final class. Can only be used in conjunction with one of the primary types above. E.g. class$.PUBLIC | class$.FINAL
extends$
Reference to base class.
implements$
Array of references to interfaces.
definition: object, required
Actual class implementation. See examples for more details.
Structure:
{
static$: {
public$: {
override$: {
}
},
protected$: {
override$: {
}
},
private$: {
override$: {
}
}
},
instance$: {
public$: {
override$: {
}
},
protected$: {
override$: {
}
},
private$: {
override$: {
}
}
}
}
static$
Static scope. All methods and properties defined within the static$ scope will be assigned to the class object.
instance$
Instance scope. All methods and properties defined within the instance$ scope will be assigned to the instance object.
public$
Public scope. All methods and properties defined within the public$ scope will be accessible from inside and outside of the class's scope as well as its inheritance chain.
protected$
Protected scope. All methods and properties defined within the protected$ scope will be accessible from inside and of the class's scope as well as its inheritance chain. They will not be accessible from outside though.
private$
Private scope. All methods and properties defined within the private$ scope will be accessible only from inside of the class's scope.
interface$(name, definition);
name: string, required
Specifies interface name. Should begin with capital 'I'. Should only contain letters and digits.
definition: object, required
Actual interface definition. See examples for more details.
Structure:
{
methodOne: function () {
},
methodTwo: function (arg1, arg2) {
}
}
Import dependency classes and interfaces.
WIP. We recommend using RequireJS for now.
Concatenates, minifies and optimises imported files into one.
Soon available!
Create new instances of defined class.
var ClassName = class$('ClassName', {type$: class$.PUBLIC}, {
});
var instance = new ClassName();
A public instance method named by the class will become the class's constructor.
var ClassName = class$('ClassName', {type$: class$.PUBLIC}, {
instance$: {
public$: {
ClassName: function () {
// constructor
}
}
}
});
Instance fields are being defined within the instance$ scope.
var ClassName = class$('ClassName', {type$: class$.PUBLIC}, {
instance$: {
public$: {
iBelongToInstance: function () {
}
}
}
});
var instance = new ClassName();
instance.iBelongToInstance();
Static fields are being defined within the static$ scope.
var ClassName = class$('ClassName', {type$: class$.PUBLIC}, {
static$: {
public$: {
iBelongToTheClass: function () {
}
}
}
});
ClassName.iBelongToTheClass();
Have full control over your methods and properties thanks to accessibility specifiers.
var ClassName = class$('ClassName', {type$: class$.PUBLIC}, {
static$: {
public$: {
iAmAPublicField: 'public',
iAmPublic: function () {
// public
}
},
protected$: {
iAmAProtectedField: 'protected',
iAmProtected: function () {
// protected
}
},
private$: {
iAmAPrivateField: 'private',
iAmPrivate: function () {
// private
}
}
},
instance$: {
public$: {
iAmAPublicField: 'public',
iAmPublic: function () {
// public
}
},
protected$: {
iAmAProtectedField: 'protected',
iAmProtected: function () {
// protected
}
},
private$: {
iAmAPrivateField: 'private',
iAmPrivate: function () {
// private
}
}
}
});
Any variable named with only upper-case-with-underscores name will be considered constant.
var ClassName = class$('ClassName', {type$: class$.PUBLIC}, {
instance$: {
public$: {
I_AM_CONSTANT: 'can not change me'
}
}
});
Extend from other classes.
var BaseClass = class$('BaseClass', {type$: class$.PUBLIC}, {
instance$: {
public$: {
iBelongToBaseClass: function () {
}
}
}
});
var SubClass = class$('SubClass', {type$: class$.PUBLIC, extends$: BaseClass}, {
});
var instance = new SubClass();
instance.iBelongToBaseClass();
Methods have to be overridden explicitly. Attempts to override a method implicitly will result in an error. Implementations from the base class can be accessed via public$ reference on the method itself.
var BaseClass = class$('BaseClass', {type$: class$.PUBLIC}, {
instance$: {
public$: {
iBelongToBaseClass: function () {
}
}
}
});
var SubClass = class$('SubClass', {type$: class$.PUBLIC, extends$: BaseClass}, {
instance$: {
public$: {
override$: {
iBelongToBaseClass: function () {
// explicit override
this.iBelongToBaseClass.super$(); // call super
}
}
}
});
var instance = new SubClass();
instance.iBelongToBaseClass();
Final classes cannot be extended.
var ClassName = class$('ClassName', {type$: class$.PUBLIC | class$.FINAL}, {
});
Abstract classes can only be extended, but cannot be instantiated directly.
var ClassName = class$('ClassName', {type$: class$.ABSTRACT}, {
});
Singleton classes have only one instance across your entire application. They cannot be instantiated directly. Instead, instance field is used to reference the class's instance.
var ClassName = class$('ClassName', {type$: class$.SINGLETON}, {
});
var instance = ClassName.instance;
You can define interfaces in the following way.
var IInterface = interface$('IInterface', {
methodOne: function (arg1, arg2) {
},
methodTwo: function () {
}
});
Use interfaces to enable class definition validation against missing methods.
var IInterface = interface$('IInterface', {
methodOne: function (arg1, arg2) {
},
methodTwo: function () {
}
});
var ClassName = class$('ClassName', {type$: class$.PUBLIC, implements$: [IInterface]}, {
instance$: {
public$: {
methodOne: function (arg1, arg2) {
},
methodTwo: function (arg2, arg2) {
}
}
}
});
You can use POOF.js with RequireJS very easily.
define([], function () {
var BaseClass = class$('BaseClass', {type$: class$.PUBLIC}, {
});
return BaseClass;
});
define(['BaseClass'], function (BaseClass) {
var SubClass = class$('SubClass', {type$: class$.PUBLIC, extends$: BaseClass}, {
});
return SubClass;
});