Classical ################## A simple cross-platform functional provider of classical inheritance for Javascript.
Including in your code
node.js
Classical methods (Class, Extend, Public, etc.) will be placed in the global scope and can be used anywhere in your code.
If you would prefer not to use Classical at the global level, you will need to set process.env.CLASSICAL_PROTECTGLOBALS
to true
, and then require classical in each class:
var Classical =
You can then use the classical variables as Classical.Class
, Classical.Extend
, Classical.Public
, etc.
browser (unminified, global scope)
In the browser, Classical requires require.js.
You must configure your requirejs to know where Classical's methods are located:
requirejs;
You can then use Classical as a normal requirement:
;
browser (unminified, local scope)
If you would prefer Classical not put itself at the window level, you will need to set window.CLASSICAL_PROTECTGLOBALS
to true
before requiring Classical.
You must still configure Classical the same way; however, there are some differences when using Classical later:
;
browser (minified, global scope)
Classical does not currently work in minified format
If you would like to use the minifed version of Classical, you must use a different requirejs config:
requirejs
browser (minified, local scope)
Classical does not currently work in minified format
You can also use the minified version of Classical without affecting the window. This is done the same way as before, using the configuration for the minfied version.
Examples
Creating a Class
; var Mammal = ;
Extension
var Dog = ;
Instantiation
var Spot = 'Spot' 'Dalmation';Spot; // Outputs: 'Spot says, "Woof!"' to the console.
API
Public
Defines a public member for a class.
Parameters
member
The definition of the member. This member will be publically available
for all instances of the class and its children.
Returns
PublicMember
A public member
Example
thisfoo = ;
Protected
Defines a protected member for a class.
Parameters
member
The definition of the member. This member will be privately available for
all instances of the class and its children.
Returns
ProtectedMember
A protected member
Example
thisfoo = ;
Private
Defines a private member for a class.
Parameters
member
The definition of the member. This member will be privately available for
all instances of the class, but will be unavailable to its children.
Returns
PrivateMember
A private member
Example
thisfoo = ;
Class
Defines a new Class.
Parameters
fn
The definition of the class, represented as a function.
Returns
Class
A factory to create instances of the class (using the new keyword)
Example
var Foo = ;var n = ;console; // => 'foo'
Extend
Extends an existing Classical or non-Classical class.
Parameters
ancestor
The non-Classical class to inherit from.
fn
The definition of the class, represented as a function.
Example
var Baz = ;var n = ;n; // => 'foo'
Interface
Creates a new interface to be implemented.
Parameters
fn
The definition of the interface, represented as a function.
Example
var Squee = ;
Implement
Implements interfaces as a classical class.
Parameters
interfaces
An interface (or several interfaces as an array).
fn
The definition of the implementing class, represented as a function.
Example
var Nop = ;