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