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

[Prev: Bit-wise operators] [Home] [Next: Declarations]

#### Special Operators

##### ?: operator

`expression ? resultIfTrue : resultIfFalse`

This operator evaluates its first operand, the expression. If the expression is `true`, the value of the second operand (resultIfTrue) is returned; otherwise the value of the third operand (resultIfFalse) is returned.

##### , operator

`expression1, expression2`

This operator evaluates its first and second operand (expression1 and expression2), and returns the value of the second operand (expression2).

The comma operator can be subtle, and is best reserved only for use in argument lists.

##### function operator

     var variable = function( optArguments ) { Statements }

This operator is used to create anonymous functions. Once assigned, the variable is used like any other function name, e.g. `variable(1, 2, 3)`. Specify the argument names (in optArguments) if named arguments are required. If no optArguments are specified, arguments may still be passed and will be available using the arguments list.

The Qt Script `function` operator supports closures, for example:

        function make_counter( initialValue )
        {
            var current = initialValue;
            return function( increment ) { current += increment; return current; }
        }
        // ...
        var counterA = make_counter( 3 );  // Start at 3.
        var counterB = make_counter( 12 ); // Start at 12.
        debug( counterA( 2 ) );  // Adds 2, so prints 5
        debug( counterB( 2 ) );  // Adds 2, so prints 14
        debug( counterA( 7 ) );  // Adds 7, so prints 12
        debug( counterB( 30 ) ); // Adds 30, so prints 44

Note that for each call to make_counter(), the anonymous function that is returned has its own copy of current (initialized to the initialValue), which is incremented independently of any other anonymous function's current. It is this capturing of context that makes the function that is returned a closure.

See also function and Function Type.

##### in operator

`property in Object`

Returns `true` if the given Object has the given property; otherwise returns `false`.

##### instanceof operator

`object instanceof type`

Returns `true` if the given object is an instance of the given type, (or of one of its base classes); otherwise returns `false`.

##### new operator

`var instance = new Type( optArguments );`

This function calls the constructor for the given Type, passing it the optional arguments (optArguments) if any, and returns an instance of the given Type. The Type may be one of the built-in types, one of the library types, or a user-defined type.

Example:

        var circle = new Circle( x, y );
        var file = new File();

##### this operator

`this.property`

The this operator may only be used within a function that is defined within a class or form, i.e. a member function. Within the scope of the function this is a reference to the particular instance (object) of the class's type.

Example:

        class MinMax {
            var _min;
            var _max;
            function MinMax( min, max ) { this._min = min; this._max = max; }
            function max() { return this._max; }
            function min() { return this._min; }
            function setMax( value ) { this._max = value; }
            function setMin( value ) { this._min = value; }
        }

##### typeof operator

`typeof item`

This operator returns a type of the object as a string.

Example:

        var f = new Function("arguments[0]*2"); // "object"
        var str = "text"; // "string"
        var pi = Math.PI; // "number"

Functions and built-in objects have a `typeof` of "function".

[Prev: Bit-wise operators] [Home] [Next: Declarations]