Supported: Internet Explorer 6+ Supported: Mozilla Firefox 2+ Supported: Apple Safari 3+ Supported: Google Chrome Supported: Opera 9+ Supported: Konqueror

OOP Engine

Preface

By its nature, JavaScript is an objective prototype-based language and has no mechanism for normal programming in the OOP-style so accustomed to the most of developers. Typically work with prototypes are often fraught with a number of inconveniences and as usual working with prototypes makes some people confused.

This library is designed to make slight adjustments in the processes of writing code by developers and get them back to the fold of such habitual object-oriented approach to writing programs.

Such terms as “class”, “inheritance”, “polymorphism,” “constructor” etc. came back while working with this library.


Declaration

Class declaration in AJAX.OOP goes through call of Ajax.Class function which included into a package:

Ajax.Class( [ParentClass,] ClassDeclaration)

Ajax.Class takes one or two arguments. If passed one argument it will be interpreted by function as Class Declaration Object, if passed two arguments - first will be interpreted as Parent Class Definition, second - as Class Declaration.

Parent Class Definition should be a reference to previously declared class with Ajax.Class function or by a native-style JavaScript definition. Actually, Parent Class Definition is a reference to an instance of Function Built-in object, which at the same time is constructor of an Object in JavaScript.

Class Declaration should be an instance of Object. In this simple way it is very easy to define both properties and methods of our new class. Look at the examples below:

Listing 1. Creating class in classic-style way:

function myClass() {
  this.x = 1;
  alert( this.x);
};

Listing 2. Creating class with Ajax.Class function:

var myClass = Ajax.Class({

  /**
   * properties declaration
   */
  x : 1,

  /**
   * methods declaration
   */
  constructor : function() {
    alert( this.x);
  }

});

As seen from listings above both methods defines the same classes but in some different ways. By the second way we create class constructor dynamically with Ajax.Class function but as result we’ve got the same object as in first example.

When declaring a class with Ajax.Class function we can define constructor inside declaration or we can do not do it. If we declare constructor then when we will instantiate the object defined constructor will be used, otherwise will be used an empty function as constructor.


Inheritance

Inheritance in AJAX.OOP goes through passing first argument as Parent Class Definition to Ajax.Class function.

Passed argument should be an instance of Function. This mean that you can use any class previously declared as with Ajax.Class as with standard JavaScrip syntax or with other packages. Below you’ll find a shor example.

Listing 3. Inheritance:    [run]

function myBaseClass() {
  this.x = 1;
  alert( 'myBaseClass.x = ' + this.x);
};

var myClass = Ajax.Class( myBaseClass, {

  /**
   * methods declaration
   */
  constructor : function( call_parent) {
    if (call_parent) {
      this.$super.constructor();
    } else {
      alert( 'myClass.x = ' + this.x);
    }
  }

});

new myClass( true);
new myClass();

Each class, defined with Ajax.Class will have this.$super property. Actually, this property is a reference to a parent prototype object, so you can easily call/access any methods or properties from a parent class definition.


Overriding

By using Ajax.Class you can write in class declaration any properties and methods which should override their parentness. If you need you can always call overriden properties and methods from parent class by using this.$super property inside class declaration. See example below:

Listing 4. Overriding:    [run]

var myBaseClass = Ajax.Class( {
  /**
   * properties declaration
   */
   x : 1
});

var myClass = Ajax.Class( myBaseClass, {
  /**
   * override property x from parent class:
   */
  x: 2,

  /**
   * methods declaration
   */
  constructor : function() {
    alert( 'myClass.x = ' + this.x);
    alert( 'myBaseClass.x = ' + this.$super.x)
  }
});

new myClass();


Aggregation

There are no limits for using aggregation under the terms of AXAJ.OOP library. You are able to aggregate any objects inside classes declaration.

By the way it is also possible to aggregate current class itself and its parents by using some special properties assigned to a class definition by Ajac.Class function.

These special properties are:

  • this.$_super - reference to a parent constructor
    To instantiate parent object just call var obj = new this.$_super();
  • this.$_self - reference to a self constructor
    To instantiate self object just call var obj = new this.$_self();