Zebra API Doc

zebra.Class()

Class

(
  • [inheritedClass]
  • [inheritedInterfaces*]
  • methods
)
Function

Class declaration method. Zebra easy OOP concept supports:

Single class inheritance. Any class can extend an another zebra class

   // declare class "A" that with one method "a"   
   var A = zebra.Class([
       function a() { ... }
   ]);

   // declare class "B" that inherits class "A"
   var B = zebra.Class(A, []);

   // instantiate class "B" and call method "a"
   var b = new B();
   b.a();

Class method overriding. Override a parent class method implementation

   // declare class "A" that with one method "a"   
   var A = zebra.Class([
       function a() { ... }
   ]);

   // declare class "B" that inherits class "A"
   // and overrides method a with an own implementation
   var B = zebra.Class(A, [
       function a() { ... }
   ]);

Class method overloading. You can declare methods with the same names but different parameter list. The methods are considered as different methods

   // declare class "A" that with one method "a"   
   var A = zebra.Class([
       function a() { ... }
   ]);

   // declare class "B" that inherits class "A"
   // and overloads method "a" with another number of
   // parameters 
   var B = zebra.Class(A, [
       function a(param1) { ... }
   ]);

   // instantiate class B and call two different 
   // methods "a()" and "a(param1)"
   var b = new B();
   b.a();      // call method defined in "A" class 
   b.a(100);   // call overloaded method defined in "B" class

Constructors. Constructor is a method with empty name

   // declare class "A" that with one constructor
   var A = zebra.Class([
       function () { this.variable = 100; }
   ]);

   // instantiate "A"
   var a = new A();
   a.variable // variable is 100 

Static methods and variables declaration. Static fields and methods can be defined by declaring special "$clazz" method whose context is set to declared class

   var A = zebra.Class([
       // special method where static stuff has to be declared
       function $clazz() {
           // declare static field
           this.staticVar = 100;
           // declare static method
           this.staticMethod = function() {};
       }
   ]); 

   // access static field an method
   A.staticVar      // 100
   A.staticMethod() // call static method 

Access to super class context. You can call method declared in a parent class

   // declare "A" class with one class method "a(p1,p2)"
   var A = zebra.Class([
       function a(p1, p2) { ... }  
   ]); 

   // declare "B" class that inherits "A" class and overrides "a(p1,p2)" method
   var B = zebra.Class(A, [
       function a(p1, p2) { 
           // call "a(p1,p2)" method implemented with "A" class
           this.$super(p1,p2); 
       }  
   ]); 

One of the powerful feature of zebra easy OOP concept is possibility to instantiate anonymous classes and interfaces. Anonymous class is an instance of an existing class that can override the original class methods with own implementations, implements own list of interfaces. In other words the class instance customizes class definition for the particular instance of the class;

       // declare "A" class
       var A = zebra.Class([
           function a() { return 1; }
       ]);

       // instantiate anonymous class that add an own implementation of "a" method
       var a = new A([
           function a() { return 2; }
       ]);
       a.a() // return 2

Parameters:

  • [inheritedClass] zebra.Class optional

    an optional parent class to be inherited

  • [inheritedInterfaces*] zebra.Interface optional

    an optional list of interfaces for the declared class to be extended

  • methods Array

    list of declared class methods. Can be empty array.


Return: Function

a class definition

extend

(
  • [interfaces*]
  • methods
)

Extend existent class with the given methods and interfaces Be careful to use the method, pay attention the following facts:

  • only the given class and the classes that inherit the class after the extend method calling get the updates
  • if the class gets method that already defined the old method will be overridden
  • "$super" cannot be called from the method the class is extended

For example:

   var A = zebra.Class([ // declare class A that defines one "a" method
       function a() {
           console.log("A:a()");
       }
   ]);

   var a = new A();
   a.a();  // show "A:a()" message

   A.extend([
       function b() {
           console.log("EA:b()");
       },

       function a() {   // redefine "a" method
           console.log("EA:a()");
       }
   ]);

   // can call b() method we just added to the instance class 
   a.b(); // show "EA:b()" message
   a.a(); // show "EA:a()" message

Parameters:

  • [interfaces*] zebra.Interface optional

    zero or N interfaces

  • methods Array

    array of the methods the class have to be extended with

getMethods

(
  • [name]
)
Array

Get declared by this class methods.

Parameters:

  • [name] String optional

    a method name. The name can be used as a filter to exclude all methods whose name doesn't match the passed name


Return: Array

an array of declared by the class methods