[Qt-qml] Semantics of 'return' in a JavaScript block in QML
aaron.kennedy at nokia.com
aaron.kennedy at nokia.com
Thu Sep 16 02:37:46 CEST 2010
Hi,
On 15/09/10 10:15 PM, "Kellomaki Pertti (Nokia-MS/Tampere)"
<pertti.kellomaki at nokia.com> wrote:
> This caught my eye when looking at examples in the qt-components project
> [1]. It looks like 'return' can be used to return a value from a script
> block:
>
> Text {
> text: { if (condition) {
> return "A";
> }
> return "B";
> }
> }
>
> My JavaScript knowledge is pretty much nonexistent, but cursory googling
> would suggest that in JavaScript, a return statement can only be used
> inside functions. Is the above syntax an extension of JavaScript, and if
> so, is it documented somewhere?
All of QML is an extension to JavaScript :) As another replier identified,
bindings in QML can be though of as a convenient function call. Internally
the above is actually evaluated like this:
Text {
text: (function() { if (condition) { return "A"; } return "B"; })()
}
We do this to allow you to write complex bindings without actually having to
go to the trouble of splitting it out into a single-use function call.
We should definitely document these things better. There is a balancing act
between filling the documentation with descriptions of technical edge cases
that will confuse most people, but are helpful for some that want to
understand things from "the ground up".
Cheers,
Aaron
More information about the Qt-qml
mailing list