- Qt 6.11
- Qt Qml
- The QML Reference
- Integrating QML and JavaScript
- Defining JavaScript Resources in QML
- Qt 6.11.1 Reference Documentation

Dynamic QML Object Creation from JavaScript ◦ JavaScript Host Environment

# Defining JavaScript Resources in QML

The program logic for a QML application may be defined in JavaScript. The JavaScript code may either be defined in-line in QML documents, or separated into JavaScript files (known as `JavaScript Resources` in QML).

There are two different kinds of JavaScript resources which are supported in QML: code-behind implementation files, and shared (library) files. Both kinds of JavaScript resource may be imported by other JavaScript resources, or included in QML modules.

## Code-Behind Implementation Resource

Most JavaScript files imported into a QML document are stateful implementations for the QML document importing them. In these cases, each instance of the QML object type defined in the document requires a separate copy of the JavaScript objects and state in order to behave correctly.

The default behavior when importing JavaScript files is to provide a unique, isolated copy for each QML component instance. If that JavaScript file does not import any resources or modules with a `.import` statement, its code will run in the same scope as the QML component instance and consequently can access and manipulate the objects and properties declared in that QML component. Otherwise, it will have its own unique scope, and objects and properties of the QML component should be passed to the functions of the JavaScript file as parameters if they are required.

An example of a code-behind implementation resource follows:

     // MyButton.qml
     import QtQuick 2.0
     import "my_button_impl.js" as Logic // A new instance of this JavaScript resource
                                         // is loaded for each instance of Button.qml.

     Rectangle {
         id: rect
         width: 200
         height: 100
         color: "red"

         MouseArea {
             id: mousearea
             anchors.fill: parent
             onClicked: Logic.onClicked(rect)
         }
     }

     // my_button_impl.js
     var clickCount = 0;   // this state is separate for each instance of MyButton
     function onClicked(button) {
         clickCount += 1;
         if ((clickCount % 5) == 0) {
             button.color = Qt.rgba(1,0,0,1);
         } else {
             button.color = Qt.rgba(0,1,0,1);
         }
     }

In general, simple logic should be defined in-line in the QML file, but more complex logic should be separated into code-behind implementation resources for maintainability and readability.

## Shared JavaScript Resources (Libraries)

By default, JavaScript files imported from QML share their context with the QML component. That means the JavaScript files have access to the same QML objects and can modify them. As a consequence, each import must have a unique copy of these files.

The previous section covers stateful imports of JavaScript files. However, some JavaScript files are stateless and act more like reusable libraries, in the sense that they provide a set of helper functions that do not require anything from where they were imported from. You can save significant amounts of memory and speed up the instantiation of QML components if you mark such libraries with a special pragma, as shown in the following example.

     // factorial.js
     .pragma library

     var factorialCount = 0;

     function factorial(a) {
         a = parseInt(a);

         // factorial recursion
         if (a > 0)
             return a * factorial(a - 1);

         // shared state
         factorialCount += 1;

         // recursion base-case.
         return 1;
     }

     function factorialCallCount() {
         return factorialCount;
     }

The pragma declaration must appear before any JavaScript code excluding comments.

Note that multiple QML documents can import `"factorial.js"` and call the factorial and factorialCallCount functions that it provides. The state of the JavaScript import is shared across the QML documents which import it, and thus the return value of the factorialCallCount function may be non-zero when called within a QML document which never calls the factorial function.

For example:

     // Calculator.qml
     import QtQuick 2.0
     import "factorial.js" as FactorialCalculator // This JavaScript resource is only
                                                  // ever loaded once by the engine,
                                                  // even if multiple instances of
                                                  // Calculator.qml are created.

     Text {
         width: 500
         height: 100
         property int input: 17
         text: "The factorial of " + input + " is: " + FactorialCalculator.factorial(input)
     }

As they are shared, .pragma library files cannot access QML component instance objects or properties directly, although QML values can be passed as function parameters.

Dynamic QML Object Creation from JavaScript ◦ JavaScript Host Environment