Namespace: ES5Class

ES5Class

Base class that should define other classes

Members

<static> $apply :Array.<Object>

The current mixin objects and function that should be applied to this class upon instantiation

Type:
  • Array.<Object>

<static> $class :ES5Class

Type:

<static> $className :String

The current class name

Type:
  • String

<static> $className :String

Get the current class name.

Gets overwritten in ES5Class.$define

Type:
  • String

<static> $implements :Array.<ES5Class, Object, function()>

Array containing mixin'd classes, objects and functions

Type:

<static> $parent :ES5Class

The parent class. This is always defined and defaults to ES5Class if not derived from any other class.

Type:

<static> constructor :ES5Class

The class definition itself

Type:

$arguments :Array

The arguments that instantiated this class

Type:
  • Array
Example
var YourClass = ES5Class.$define('YourClass');
YourClass.$create(1,2,{'3': 4}).$arguments // [1,2,{'3':4}]

$class :ES5Class

Get the current class definition created with ES5Class.$define

Type:

$class :ES5Class

The constructor of this class

Type:

$class :ES5Class

Type:

$className :String

Type:
  • String
See:

$implements :Array

Type:
  • Array
See:

$names :Array

Shortcut for Object.getOwnPropertyNames(this)

Type:
  • Array
Example
var YourClass = ES5Class.$define('YourClass',{ protoName: true });
YourClass.$create().$names; // ['protoName']

$parent :ES5Class

The parent prototype

Type:

Methods

<static> $const(values, toPrototype) → {ES5Class}

Define enumerable but read only methods or fields

Parameters:
Name Type Description
values function | Object

Object containing the key: values, or a function that returns an object

toPrototype Boolean

Append to this class prototype instead

Returns:

Return itself so it can be chained

Type
ES5Class
Example
var MyClass = ES5Class.$define('MyClass');
MyClass.$const({
  myValue: 1
});
MyClass.myValue = 0; // MyClass.myValue is still 1
//throws Error if in strict mode

<static> $create() → {ES5Class}

Create a new instance of your class. It returns the instance, so you can chain methods.

Parameters:
Name Type Argument Description
... * <optional>
<repeatable>

Any parameters

Returns:

The instance of your class

Type
ES5Class
Example
var YourDefinedClass = ES5Class.$define('YourDefinedClass', {
  instanceMethod: function(){}
});
YourDefinedClass.$create('some', 'arguments').instanceMethod();

<static> $define(className, include, implement) → {ES5Class}

Define a new class

Parameters:
Name Type Argument Description
className String

The name of the class

include Object | function <optional>

Your class prototype functions and variables or closure

implement Object | function <optional>

Your class static methods or closure

Throws:
Error if you don't define a name for your class
Returns:

Your class definition

Type
ES5Class
Example
var NewClass = ES5Class.$define('name', {
  construct: function(){
  }
}, {
  static: 1
});
// If you use a function, you need to return an object
var NewClass = ES5Class.$define('name', function(){
 // private variables

 return {
   construct: function(){
   }
 };
});

<static> $implement(obj, apply, importing, both) → {ES5Class}

Add, override or overload static methods to the class

Parameters:
Name Type Description
obj Object | function

The definition object or closure

apply Boolean

When implementing other classes, you can automatically apply their constructors by passing true to this parameter

importing Boolean

Is being called from an instantiated class, the mixin is made per instance and not globally

both Boolean

Should import both prototype and properties

Returns:

Returns itself so it can be chained

Type
ES5Class

<static> $include(obj) → {ES5Class}

Add, override or overload prototype methods of the class

Parameters:
Name Type Description
obj Object | function

The definition object or closure

Returns:

Returns itself so it can be chained

Type
ES5Class

<static> $inherit(from, args) → {ES5Class}

Inherits from an existing class, apply the constructor automatically

Parameters:
Name Type Description
from function | Object

The class / object to inherit from

args Array

Arguments when this super constructor is called. If you leave it undefined, it will apply the arguments you pass to the $create function. If you pass in an empty array, the super constructor will be called with no arguments.

Returns:
Type
ES5Class
Example
var MyEventEmitter = ES5Class.$define('MyEventEmitter');
MyEventEmitter.$inherit(require('events').EventEmitter, []);

<static> $isClass(cls) → {Boolean}

Check if the current class definition is the param

Parameters:
Name Type Description
cls Object

Any class

Returns:

Whether is the given class or not

Type
Boolean

<static> $isClass(cls) → {Boolean}

Parameters:
Name Type Description
cls Object
Returns:

If the current is the given param

Type
Boolean

<static> $isES5Class(cls) → {Boolean}

Check if the argument is a ES5Class

Parameters:
Name Type Description
cls function | Object

Object or function to check if is an ES5Class

Returns:

Given parameter is an ES5Class?

Type
Boolean

<static> $wrap(obj, name) → {ES5Class}

Ensure the given param is an ES5Class before trying to call any ES5Class specific functions.

Parameters:
Name Type Argument Description
obj *

Object to wrap as a function

name String <optional>

Name of the object if it's not already an ES5Class

Returns:

Always return a class, even an empty one

Type
ES5Class
Example
var MyClass = {'doh': true};
ES5Class.$isES5Class(MyClass); // false
ES5Class.$isES5Class(ES5Class.$wrap(MyClass)); // true, named Wrapped
ES5Class.$isES5Class(ES5Class.$wrap(MyClass, 'MyClass')); // true, named MyClass

$delegate(where) → {BetterCurry.delegate}

Delegates a function or getter/setter to the constructor static method or to a member of the current instance

This is an expensive operation and shouldn't be used in performance critical parts of the application. Instead, you should inherit from a deriving class if you need performance.

Parameters:
Name Type Argument Description
where String <optional>

Where to delegate, any member of the current instance or defaults to $class

Returns:

An instance of BetterCurry.delegate

Type
BetterCurry.delegate
Example
// by default, you create a delegate from the static members of the class
var YourClass = ES5Class.$define('YourClass', {}, {
  someFunction: function(){
  },
  someStaticObject: {}
});
var instance = YourClass.$create();
instance.$delegate().access('someStaticObject').method('someFunction');

instance.someFunction(); // calls the YourClass.someFunction like if it belong to the prototype

// but you can create it from another member of the current instance
YourClass.$include({
  someData: {
   subdata: {
     //...
   }
  }
});
instance = YourClass.$create();
instance.$delegate('someData').access('subdata');
instance.subdata.addMember = true; // instance.someData.subdata.addMember === true
//instance.subdata is now a read/write access to the original someData.subdata

$destroy() → {ES5Class}

Cleanup any variables that might hold external objects on the current instance before getting rid of it

Returns:

Returns itself, so it can be chained

Type
ES5Class

$exchange(obj, params) → {ES5Class}

Exchanges current proto for something else. The original prototype chain is lost.

This is an expensive operation and shouldn't be used in performance critical parts of the application.

Parameters:
Name Type Description
obj Object | function

Object that has a prototype to exchange to

params Array

Params for calling the original constructor

Returns:

Returns itself so it can be chained

Type
ES5Class
Example
var MyClass = ES5Class.$define('MyClass', {
 that: function(){}
});
MyClass.$create().$exchange(require('http').Server); // MyClass is now an instanceof Server

$import(obj) → {ES5Class}

Imports another object (both static and prototype methods) to the current instance. It mixin other objects on the current instance and execute their functions in this class context.

This is an expensive operation and shouldn't be used in performance critical parts of the application

Parameters:
Name Type Description
obj function | Object

Pass an object or a function that has a prototype or members

Returns:

Returns itself so it can be chained

Type
ES5Class
Example
var YourClass = ES5Class.$define('YourClass');
var instance = new YourClass();
instance.$import(Error); // make your instance act like if it was an Error

$instanceOf(object) → {Boolean}

Returns true if the current instance is an instance of the class definition passed parameter. It goes beyond strict isPrototypeOf check, it also check for mixin'd classes (through ES5Class.$implement or ES5Class.$inherit).

Parameters:
Name Type Description
object function | Object

You pass in either an object or function (that is a ES5Class) for checking

Returns:

Whether the current instance is instanceof the passed argument

Type
Boolean