[Qt-qml] Semantics of 'return' in a JavaScript block in QML
Gregory Schlomoff
gregory.schlomoff at gmail.com
Wed Sep 15 18:03:46 CEST 2010
>
> where the last evaluated statement is used as return value. This is
> specially useful for one-liners like the one below.
> Text {
text: condition ? "A" : "B"
}
While we are on this topic, you may want to check out this article.
http://javascriptweblog.wordpress.com/2010/07/26/no-more-ifs-alternatives-to-statement-branching-in-javascript/
It gives an idea of some really cool tricks that you can do in JS
2010/9/15 Eduardo Fleury <eduardo.fleury at openbossa.org>
> Hi Pertti.
>
>
> 2010/9/15 Pertti Kellomäki <pertti.kellomaki at nokia.com>
>
>> Text {
>> text: { if (condition) {
>> return "A";
>> }
>> return "B";
>> }
>> }
>>
>> (...) 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?
>>
>
> My understanding is that in QML script blocks are implicit functions, and
> the above should be simply a more concise version of:
>
> Text {
> function foobar() {
>
> if (condition)
> return "A";
> return "B";
> }
>
> text: foobar()
> }
>
> Note that there's also a third valid version:
>
> Text {
> text: {
> if (condition)
> "A"
> else
> "B"
> }
> }
>
> where the last evaluated statement is used as return value. This is
> specially useful for one-liners like the one below.
>
> Text {
> text: condition ? "A" : "B"
> }
>
> Cheers,
> Eduardo
>
> --
> Eduardo M. Fleury
> OpenBossa - INdT
> http://eduardofleury.com/
> http://www.openbossa.org/
>
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-qml
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100915/86594742/attachment.html
More information about the Qt-qml
mailing list