|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications |

[Prev: Declarations] [Home] [Next: const]

#### class

    class ClassName {
        static var ClassVariable;
        var MemberVariable;
        static function ClassFunction { Statements; }
        function ClassName() { Statements; } // constructor
        function MemberFunction() { Statements; }
    }

This keyword is used to define new classes. After the keyword `class` comes the ClassName, then optionally, the keyword `extends` followed by a class name from which this class derives, then an opening brace. Class variables are declared with `static` var (ClassVariable). Only one instance of these variables exists per class. Member variables (MemberVariable), are declared with var; each instance (object) of the class has its own copy of each member variable. Functions declared with the keywords `static` function are class functions (ClassFunction); these functions are called using the name of the class rather than from an object. In the standard library, the Math functions are all static, for example `Math.sin()`. Member functions (methods) are called by objects and are declared with function. The constructor is the member function with the same name as the class; constructors must not contain an explicit return statement, or have an explicit return type, since Qt Script for Applications handles these automatically.

A class that only contains `static` const, var and function definitions does not need a constructor.

Example:

        class Area {
            static var count = 0;
            var _name;
            function Area( name ) { this._name = name; this.count++ }
            function destroy() { this.count--; }
            static function count() { return this.count; }
            function name() { return this._name; }
            function setName( name ) { this._name = name; }
        }

In this example we define the "Area" class. When an instance of the class is constructed:

        var area = new Area( "Berkshire" );

the given name is assigned to the object's `_name` variable, and the class's `static` `count` is incremented. The `destroy()` function is not called automatically; it must be called explicitly if there is any clean-up to do.

All the class's variables and functions must be defined within the class definition. ECMAScript does not have destructors. But for Qt Script for Applications forms, Qt Script for Applications will call a class's `destroy()` function, if it has one, before deleting the form. This occurs because Qt Script for Applications automatically connects the form's `destroyed()` signal to the `destroy()` function. If you want a destructor you must create your own and call it explicitly.

Classes are all derived from Object. It is possible to derive a class from another class using the `extends` keyword, for example:

        class City extends Area {
            var _population;
            function City( name, population )
            {
                Area( name );
                _population = population;
            }
            function population() { return _population; }
            function setPopulation( population ) { _population = population; }
        }

See also function.

[Prev: Declarations] [Home] [Next: const]