Skip to content
This repository was archived by the owner on Sep 21, 2020. It is now read-only.

Class_API

rdking edited this page Dec 6, 2014 · 3 revisions

#Class.js

Provides a factory for creating well-defined Classes using a classic object-oriented approach, without requiring a script compiler.

###Setup If using some form of CommonJS, then add the following to your JavaScript file:

var Class = require("Class");

Otherwise, include the following in your HTML file:

<script type='text/javascript' src='scripts/Enum.js'></script>
<script type='text/javascript' src='scripts/Functor.js'></script>
<script type='text/javascript' src='scripts/WeakMap.js'></script>
<script type='text/javascript' src='scripts/Class.js'></script>

You might also want to add the following to the top of any JavaScript file using Class.js to reduce typing:

var Private = Class.Private;
var Protected = Class.Protected;
var Public = Class.Public;
var Static = Class.Static;
var Final = Class.Final;
var Property = Class.Property;

###Constructor

new Class(<definition>);
new Class(<objectName>, <definition>);

#####Parameters:

  • objectName: The string name of the new Class. Can be omitted.
  • definition: An object representing the actual class declaration. Required.

#####Returns: Returns a new Class constructor suitable for creating instances of the newly defined type.

###The Class Definition A Class definition is a simple Javascript object, functionally notated to scope and status to each member. To do this, Class.js provides you with 6 functions that work together.

#####Privilege Levels:

  • Class.Private(data): Declare that data has private scope (i.e. is only visible from inside the class.)
  • Class.Protected(data): Declare that data has protected scope (i.e. is only visible from inside the class and its subclasses.)
  • Class.Public(data): Declare that data has public scope (i.e. is visible both inside and outside of the class and it subclasses.)

#####Member Status:

  • Class.Static(data): Declare that data is static (i.e. can be accessed without using a class instance, and having its this point to the class' static container.)
  • Class.Final(data): Declare that data is final or const (i.e. the variable holding it cannot be changed. If data is an object, its public members can be altered.)
  • Class.Property(data): Declare that data is a property (i.e. Object.defineProperty will be used to set up this element. Only get, set, and value are valid. All other properties take their values from the member status.)

Specifying more than one privilege level is an error. Not specifying a privilege level defaults to public scope. Specifying more than one status is allowed. As an example, to create a Class with a private, static value named foo, and initialized to 3:

var Test = new Class("Test", {
    foo: Private(Static(3)),
    ...
});

#####Built-ins: Class.js also looks for certain keywords in the definition object.

  • Base: Declares that the new class inherits from the specified object. That object must be a Class instance.
  • Events: Declares the list of names for the events that can be fired by the Class instance. These events appear in an Enum externally on the Class as:

<className>.Events.<eventName>

and internally on the Class as

this.Events.<eventName>

  • Constructor: Declares the instance constructor function for this class. This function is optional, and can be declared with any Privilege Level.

  • StaticConstructor: Declares the class initializer function for this class. This function runs only once at the end of the Class function's operation, just before the new constructor is returned. This constructor only ever has access to the static scope.

All 4 of these builtins are not made available in their original form on instances of Classes.

###Other useful bits...

#####External extras

  • <className>.InheritsFrom: Returns the value of Base from the class definition.
  • <className>.isClass: Will always return true for instances of new Class().
  • **<instance>.isClassInstance: Will always return true on instances of a defined Class.

#####Internal extras:

  • this.InheritsFrom: The value of Base from the class definition.
  • this.__isClassDomain: True if called from the instance's privileged scope.
  • this.__name: Name of the Class as defined by the declaration.
  • this.Delegate(fn): Returns a Functor locked to the current object given a function definition or a function member name.
  • this.Self: The reference to the public face of the object.
  • this.Events: An Enum whose members are as declared on the Events built-in in the class definition.
  • this.base: Provides access to the original versions of the public members of the base class.
Clone this wiki locally