Next: , Up: Classes


2.1 Defining Classes

C = Class( string name, Object dfn )
Define named class C identified by name described by dfn.
C = Class( string name ).extend( Object dfn )
Define named class C identified by name described by dfn.
C = Class( Object dfn )
Define anonymous class C as described by dfn.
C = Class.extend( Object dfn )
Define anonymous class C as described by dfn.

Class C can be defined in a number of manners, as listed above, provided a definition object dfn containing the class members and options. An optional string name may be provided to set an internal identifier for C, which may be used for reflection and error messages. If name is omitted, C will be declared anonymous.

Class must be imported (see Including) from easejs.Class; it is not available in the global scope.

2.1.1 Definition Object

dfn = { '[keywords] name': value[, ...] }
Define definition object dfn containing a member identified by name, described by optional keywords with the value of value. The member type is determined by typeof value. Multiple members may be provided in a single definition object.

The definition object dfn has the following properties:

  1. The keys represent the member declaration, which may optionally contain one or more keywords delimited by spaces. A space must delimit the final keyword and name.
    1. keywords must consist only of recognized tokens, delimited by spaces.
    2. Each token in keywords must be unique per name.
  2. The value represents the member definition, the type of which determines what type of member will be declared.
    1. A value of type function will define a method, which is an invokable member whose context is assigned to the class or class instance depending on keywords.
    2. All other types of value will define a property - a mutable value equal to value, assigned to a class or instance depending on keywords. Properties may be made immutable using keywords.
    3. Getters/setters may be defined in an ECMAScript 5 or greater environment. Getters/setters must share the same value for keywords.
  3. name must be unique across all members of dfn.

2.1.2 Member Validations

For any member name:

For any member name declared as a method, the following must hold true:

2.1.3 Discussion

In Figure 2.1, we saw how one would conventionally declare a class-like object (a prototype) in JavaScript. This method is preferred for many developers, but it is important to recognize that there is a distinct difference between Prototypal and Classical Object-Oriented development models. Prototypes lack many of the conveniences and features that are provided by Classical languages, but they can be emulated with prototypes. As an Object-Oriented developer, you shouldn't concern yourself with how a class is declared in JavaScript. In true OO fashion, that behavior should be encapsulated. With ease.js, it is.

Let's take a look at how to declare that exact same class using ease.js:

    var Class = require( 'easejs' ).Class;

    var MyClass = Class(
    {
        'public prop': 'foobar',

        'public getProp': function()
        {
            return this.prop;
        }
    } );

    // create a new instance of the class and execute doStuff()
    var foo = MyClass();
    console.log( foo.getProp() ); // outputs "foobar"

Figure 2.2: Basic anonymous class declaration using ease.js

That should look much more familiar to Object-Oriented developers. There are a couple important notes before we continue evaluating this example:

The above example declares an anonymous class, which is stored in the variable MyClass. By convention, we use CamelCase, with the first letter capital, for class names (and nothing else).