9. Scripting

Jitsu apps use JavaScript for scripting. The Jitsu page compiler also includes a JavaScript preprocessor, which adds a few additional features to the JavaScript language - you can choose to adopt these or not in your projects.

JavaScript 2.0

Scripts in Jitsu are processed via the Jitsu script preprocessor, which adds a handful of extra features to JavaScript.

In future releases, we are considering extending the preprocessor to use a subset of the ECMAScript 4 language (JavaScript 2.0). That way, you could code Jitsu in JavaScript 2.0 and then down-compile to JavaScript 1.5 for browsers.

#class, #method, #call

#class, #method and #call are macros that are translated it into one or more lines of JavaScript. You can use these macros in any script that is compiled via the page compiler, or imported through usings.

If you use them as described below they can save you a lot of typing. However, they are entirely optional - you can always use the full JavaScript version if you prefer. We've found that these versions save typing and are less error prone.

#class

The #class directive creates a new JavaScript class. Writing:

   #class MyClass(args) : ABaseClass { 
       // constructor code here
   }

Is a shorthand for writing the JavaScript:

    function MyClass(args) { 
       // constructor code here  
    }
    MyClass.prototype = new ABaseClass();
    MyClass.prototype.constructor = MyClass;
    MyClass.typeName = 'MyClass';
    MyClass.baseType = ABaseClass;    

This declares the class function, initializes the class prototype to point to a new instance of the base class, remembers the constructor in a property called "constructor", and also records some useful class information on the class function.

#method

The #method directive adds a method to a class. For example:

    #method MyClass.tingle(args) {
         // method body here
    }   

For projects compiled in Debug mode, #method is expanded to the following JavaScript:

    MyClass.prototype.tingle = function MyClass_tingle(args) {   
         // method body here
    }   

For projects compiled in Release mode, #method is replaced by the shorter:

    MyClass.prototype.tingle = function(args) {   
         // body here
    }   

(The former version is more helpful when you are debugging and want to get error messages that tell you the name of the function that caused the error).

Within a control definition in an Xml file, the classname part is optional - it is automatically set to the name of the control class being defined. Outside of a control definition, the class name is required.

#call

The #call directive can be used in JavaScript code to call a base classes' implementation of a method. For example:

    /* define a base class with a test() method. */ 
    #class  ABaseClass()      {}    
    #method ABaseClass.test() { alert('hello'); }

    /* define MyClass, which extends ABaseClass */ 
    #class MyClass() : ABaseClass { }

    #method MyClass.test() {    
        // Call the base class version of test()
        #call ABaseClass.test();

        // Now do some more work...
    }

The #call ABaseClass.test(...) is expanded to:

    ABaseClass.prototype.test.call(this, ...);

Which invokes the superclasses implementation of the example method.

The ID Manager

IdManager provides a way to handle symbolic strings in a JavaScript application.

To learn more about the ID manager, see The Cruncher.