46Type annotations help tools like \l{\QC Documentation}{\QC}
47and \l{qmllint} to make sense
48of the code and provide better diagnostics. Moreover, they make functions easier
49to use from C++. See
50\l {qtqml-cppintegration-interactqmlfromcpp.html}{Interacting with QML Objects from C++}
51for more information.
52
53The exception to this rule are functions assigned to signal handlers: There, type annotations are
54forbidden to avoid a potential mismatch with the types of the signal. This does not cause issues for
55tooling, as the signal already provides the necessary information.
56
57\note In QML, enumerations are not types and can therefore not be used as type annotations. Their
58underlying numeric type, int or double, should be used instead.
59
60Type assertions (sometimes called \e as-casts) can also be used in order to cast an object to a
61different object type. If the object is actually of the given type, then the type assertion returns
62the same object. If not, it returns \c null. In the following snippet we assert that the \c parent
63object is a \c Rectangle before accessing a specific member of it.
64
65\qml
66Item {
67 property color parentColor: (parent as Rectangle)?.color || "red"
68}
69\endqml
70
71The optional chaining (\c{?.}) avoids throwing an exception if the parent is
72actually not a rectangle. In that case "red" is chosen as \c parentColor.
73
74Since Qt 6.7 type annotations are always enforced when calling functions. Values
75are coerced to the required types as necessary. Previously, type annotations were
76ignored by the interpreter and the JIT compiler, but enforced by \l{qmlcachegen}
77and \l{qmlsc} when compiling to C++. This could lead to differences in behavior in
78some corner cases. In order to explicitly request the old behavior of the
79interpreter and JIT you can add the following to your QML document:
80
81\qml
82pragma FunctionSignatureBehavior: Ignored
83\endqml
84
85\section1 QML Global Object
86
87The QML JavaScript host environment implements a number of host objects and functions, as
88detailed in the \l{QML Global Object} documentation.
89
90These host objects and functions are always available, regardless of whether any modules
91have been imported.
92
93
94\section1 JavaScript Objects and Functions
95
96A list of the JavaScript objects, functions and properties supported by the
97QML engine can be found in the \l{List of JavaScript Objects and Functions}.
98
99Note that QML makes the following modifications to native objects:
100
101\list
102\li An \l {string}{arg()} function is added to the \c String prototype.
103\li Locale-aware conversion functions are added to the \l{The Date JavaScript Object}{Date} and \l{The Number JavaScript Object}{Number} prototypes.
104\endlist
105
106See also:
107\list
108\li \l{The Date JavaScript Object}{Number} - The Number JavaScript object
109\li \l{The Date JavaScript Object}{Date} - The Date JavaScript object
110\li \l{The XMLHttpRequest JavaScript Object}{XMLHttpRequest} - The XMLHttpRequest JavaScript object
111\endlist
112
113In addition, QML also extends the behavior of the instanceof function to
114allow for type checking against QML types. This means that you may use it to
115verify that a variable is indeed the type you expect, for example:
116
117\qml
118 var v = something();
119 if (!v instanceof Item) {
120 throw new TypeError("I need an Item type!");
121 }
122
123 ...
124\endqml
125
126
127\section1 JavaScript Environment Restrictions
128
129QML implements the following restrictions for JavaScript code:
130
131\list
132\li JavaScript code written in a \c .qml file cannot modify the global object.
133 JavaScript code in a .js file can modify the global object,
134 and those modifications will be visible to the .qml file when
135 \l {Importing a JavaScript Resource from a QML Document}{imported}.
136
137In QML, the global object is constant - existing properties cannot be modified
138or deleted, and no new properties may be created.
139
140Most JavaScript programs do not intentionally modify the global object.
141However, JavaScript's automatic creation of undeclared variables is an implicit
142modification of the global object, and is prohibited in QML.
143
144Assuming that the \c a variable does not exist in the scope chain, the
145following code is illegal in QML:
146
147\code
148// Illegal modification of undeclared variable
149a = 1;
150for (var ii = 1; ii < 10; ++ii)
151 a = a * ii;
152console.log("Result: " + a);
153\endcode
154
155It can be trivially modified to this legal code.
156
157\code
158var a = 1;
159for (var ii = 1; ii < 10; ++ii)
160 a = a * ii;
161console.log("Result: " + a);
162\endcode
163
164Any attempt to modify the global object - either implicitly or explicitly - will
165cause an exception. If uncaught, this will result in a warning being printed,
166that includes the file and line number of the offending code.
167
168\li Global code is run in a reduced scope.
169
170During startup, if a QML file includes an external JavaScript file with "global"
171code, it is executed in a scope that contains only the external file itself and
172the global object. That is, it will not have access to the QML objects and
173properties it \l {Scope and Naming Resolution}{normally would}.
174
175Global code that only accesses script local variables is permitted. This is an