234The \c jsFunction() is called whenever the TapHandler's \c tapped signal
235is emitted.
236
237See \l{qtqml-syntax-signals.html}
238{Connecting Signals to Methods and Signals} for more information.
239
240\section1 JavaScript in application startup code
241
242It is occasionally necessary to run some imperative code at application (or
243component instance) startup. While it is tempting to just include the startup
244script as \e {global code} in an external script file, this can have severe
245limitations as the QML environment may not have been fully established. For
246example, some objects might not have been created or some
247\l {Property Binding}{property bindings} may not have been established. See
248\l {JavaScript Environment Restrictions} for the exact limitations of global
249script code.
250
251A QML object emits the \c{Component.completed} \l{Signal and Handler Event
252System#Attached Signal Handlers}{attached signal} when its instantiation is
253complete. The JavaScript code in the corresponding \c{Component.onCompleted}
254handler runs after the object is instantiated. Thus, the best place to write
255application startup code is in the \c{Component.onCompleted} handler of the
256top-level object, because this object emits \c{Component.completed} when the
257QML environment is fully established.
258
259For example:
260
261\qml
262import QtQuick 2.0
263
264Rectangle {
265 function startupFunction() {
266 // ... startup code
267 }
268
269 Component.onCompleted: startupFunction();
270}
271\endqml
272
273Any object in a QML file - including nested objects and nested QML component
274instances - can use this attached property. If there is more than one
275\c onCompleted() handler to execute at startup, they are run sequentially in
276an undefined order.
277
278Likewise, every \c Component emits a \l {Component::destruction}{destruction()}
279signal just before being destroyed.
280
281*/
282
283/*
284 \internal
285 NOTE: TODO Qt 5.1: We are not sufficiently confident about the implementation of scarce
286 resources in Qt 5.0.0, so mark this section as internal for now.
287 It should eventually become public API
288
289 There is another section about scarce resources in valuetypes.qdoc. It should
290 be enabled at the same time.
291
292
293
294\section1 Scarce Resources in JavaScript
295
296As described in the documentation for \l{QML Value Types}, a \c var type
297property may hold a \e{scarce resource} (image or pixmap). There are several
298important semantics of scarce resources which should be noted:
299
300\list
301\li By default, a scarce resource is automatically released by the declarative engine as soon as evaluation of the expression in which the scarce resource is allocated is complete if there are no other references to the resource
302\li A client may explicitly preserve a scarce resource, which will ensure that the resource will not be released until all references to the resource are released and the JavaScript engine runs its garbage collector
303\li A client may explicitly destroy a scarce resource, which will immediately release the resource
304\endlist
305
306In most cases, allowing the engine to automatically release the resource is
307the correct choice. In some cases, however, this may result in an invalid
308variant being returned from a function in JavaScript, and in those cases it
309may be necessary for clients to manually preserve or destroy resources for
310themselves.
311
312For the following examples, imagine that we have defined the following class: