Next: Inheritance, Up: Classes [Contents]
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:
- 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.
- keywords must consist only of recognized tokens, delimited by spaces.
- Each token in keywords must be unique per name.
- The value represents the member definition, the type of which
determines what type of member will be declared.
- 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. - 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.
- Getters/setters may be defined in an ECMAScript 5 or greater environment. Getters/setters must share the same value for keywords.
- A value of type
- name must be unique across all members of dfn.
2.1.2 Member Validations
For any member name:
- keywords of member name may contain only one access modifier (see Access Modifiers).
- See Member Keywords for keywords restrictions.
For any member name declared as a method, the following must hold true:
- keywords of member name may not contain
override
without a super method of the same name (see Inheritance). - keywords of member name may not contain both
static
andvirtual
keywords (see Static Members and Inheritance). - keywords of member name may not contain the
const
keyword. - For any member name that contains the keyword
abstract
in keywords, class C must instead be declared as anAbstractClass
(see Abstract Classes).
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"
That should look much more familiar to Object-Oriented developers. There are a couple important notes before we continue evaluating this example:
- The first thing you will likely notice is our use of the
public
keyword; this is optional (the default visibility is public); it may be omitted for a more traditional JavaScript feel. We will get more into visibility later on (see Access Modifiers). - Unlike Figure 2.1, we do not use the
new
keyword in order to instantiate our class. You are more than welcome to use thenew
keyword if you wish, but it is optional when using ease.js. This is mainly because without this feature, if the keyword is omitted, the constructor is called as a normal function, which could have highly negative consequences. This style of instantiation also has its benefits, which will be discussed later on. - ease.js’s class module is imported using
require()
in the above example. If using ease.js client-side (see Client-Side Include), you can instead use ‘var Class = easejs.Class’. From this point on, importing the module will not be included in examples.
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).
• Class Caveats: | Important things to note about using ease.js classes | |
• Anonymous vs. Named Classes: | ||
• Constructors: | How to declare a constructor | |
• Temporary Classes: | Throwaway classes that only need to be used once | |
• Temporary Instances: | Throwaway instances that only need to be used once |
Next: Inheritance, Up: Classes [Contents]