JavaScript Host Environment | Qt QML 5.15.18 (original) (raw)

QML provides a JavaScript host environment tailored to writing QML applications. This environment is different from the host environment provided by a browser or a server-side JavaScript environment such as Node.js. For example, QML does not provide a window object or DOM API as commonly found in a browser environment.

Common Base

Like a browser or server-side JavaScript environment, the QML runtime implements the ECMAScript Language Specification standard. This provides access to all of the built-in types and functions defined by the standard, such as Object, Array, and Math. The QML runtime implements the 7th edition of the standard.

Since Qt 5.15 Nullish Coalescing is also implemented in the QML runtime.

The standard ECMAScript built-ins are not explicitly documented in the QML documentation. For more information on their use, please refer to the ECMA-262 7th edition standard or one of the many online JavaScript reference and tutorial sites, such as the W3Schools JavaScript Reference (JavaScript Objects Reference section). Many sites focus on JavaScript in the browser, so in some cases you may need to double check the specification to determine whether a given function or object is part of standard ECMAScript or specific to the browser environment. In the case of the W3Schools link above, the JavaScript Objects Reference section generally covers the standard, while the Browser Objects Reference and HTML DOM Objects Reference sections are browser specific (and thus not applicable to QML).

QML Global Object

The QML JavaScript host environment implements a number of host objects and functions, as detailed in the QML Global Object documentation.

These host objects and functions are always available, regardless of whether any modules have been imported.

JavaScript Objects and Functions

A list of the JavaScript objects, functions and properties supported by the QML engine can be found in the List of JavaScript Objects and Functions.

Note that QML makes the following modifications to native objects:

In addition, QML also extends the behavior of the instanceof function to allow for type checking against QML types. This means that you may use it to verify that a variable is indeed the type you expect, for example:

var v = something(); if (!v instanceof Item) { throw new TypeError("I need an Item type!"); }

...

JavaScript Environment Restrictions

QML implements the following restrictions for JavaScript code: