aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/src/javascript/hostenvironment.qdoc
blob: c22c392b80d3e720372dbe7c80698ae573d3f2f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\page qtqml-javascript-hostenvironment.html
\title JavaScript Host Environment
\brief Description of the JavaScript host environment provided by the QML engine


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 \c window object or \c{DOM API} as commonly found in a browser environment.

\section1 Common Base

Like a browser or server-side JavaScript environment, the QML runtime implements the
\l{ECMA-262}{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.

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 \l{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 \c{JavaScript Objects
Reference} section generally covers the standard, while the \c{Browser Objects Reference} and \c{HTML DOM
Objects Reference} sections are browser specific (and thus not applicable to QML).


\section1 QML Global Object

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

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


\section1 JavaScript Objects and Functions

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

Note that QML makes the following modifications to native objects:

\list
\li An \l {String::arg}{arg()} function is added to the \l String prototype.
\li Locale-aware conversion functions are added to the \l Date and \l Number prototypes.
\endlist

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:

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

    ...
\endqml


\section1 JavaScript Environment Restrictions

QML implements the following restrictions for JavaScript code:

\list
\li JavaScript code written in a \c .qml file cannot modify the global object.
    JavaScript code in a .js file can modify the global object,
    and those modifications will be visible to the .qml file when
    \l {Importing a JavaScript Resource from a QML Document}{imported}.

In QML, the global object is constant - existing properties cannot be modified
or deleted, and no new properties may be created.

Most JavaScript programs do not intentionally modify the global object.
However, JavaScript's automatic creation of undeclared variables is an implicit
modification of the global object, and is prohibited in QML.

Assuming that the \c a variable does not exist in the scope chain, the
following code is illegal in QML:

\code
// Illegal modification of undeclared variable
a = 1;
for (var ii = 1; ii < 10; ++ii)
    a = a * ii;
console.log("Result: " + a);
\endcode

It can be trivially modified to this legal code.

\code
var a = 1;
for (var ii = 1; ii < 10; ++ii)
    a = a * ii;
console.log("Result: " + a);
\endcode

Any attempt to modify the global object - either implicitly or explicitly - will
cause an exception.  If uncaught, this will result in a warning being printed,
that includes the file and line number of the offending code.

\li Global code is run in a reduced scope.

During startup, if a QML file includes an external JavaScript file with "global"
code, it is executed in a scope that contains only the external file itself and
the global object.  That is, it will not have access to the QML objects and
properties it \l {Scope and Naming Resolution}{normally would}.

Global code that only accesses script local variables is permitted.  This is an
example of valid global code.

\code
var colors = [ "red", "blue", "green", "orange", "purple" ];
\endcode

Global code that accesses QML objects will not run correctly.

\code
// Invalid global code - the "rootObject" variable is undefined
var initialPosition = { rootObject.x, rootObject.y }
\endcode

This restriction exists as the QML environment is not yet fully established.
To run code after the environment setup has completed, see
\l {JavaScript in Application Startup Code}.

\li The value of \c this is undefined in QML in the majority of contexts.

The \c this keyword is supported when binding properties from JavaScript.
In QML binding expressions, QML signal handlers, and QML declared functions,
\c this refers to the scope object. In all other situations, the value of
\c this is undefined in QML.

To refer to a specific object, provide an \c id.  For example:

\qml
Item {
    width: 200; height: 100
    function mouseAreaClicked(area) {
        console.log("Clicked in area at: " + area.x + ", " + area.y);
    }
    // This will pass area to the function
    MouseArea {
        id: area
        y: 50; height: 50; width: 200
        onClicked: mouseAreaClicked(area)
    }
}
\endqml

\sa {Scope and Naming Resolution}

\endlist



*/