[Qt-qml] Semantics of 'return' in a JavaScript block in QML
Pertti Kellomäki
pertti.kellomaki at nokia.com
Wed Sep 29 13:19:47 CEST 2010
On 09/29/2010 01:50 PM, ext Stephen Kelly wrote:
> Pertti Kellomäki wrote:
>
>
>> On 09/16/2010 03:37 AM, Kennedy Aaron (Nokia-MS-Qt/Brisbane) wrote:
>>
>>> Internally
>>> the above is actually evaluated like this:
>>>
>>> Text {
>>> text: (function() { if (condition) { return "A"; } return "B"; })()
>>> }
>>>
>>>
>> This would actually be a good way to document it, at least to a former
>> Lisp-head like myself ;-)
>>
>
> As a C++ head I rarely see syntax like the above and I find it quite
> difficult to parse.
Me too. It would be a lot clearer as
(lambda () (if condition "A" "B"))()
;-)
> I've seen stuff like that when reading jQuery.js and
> code that uses it, but what is actually happening and why is it such a
> common construct when using javascript? Got links?
>
> It looks like a unnamed function is defined 'inline' inside some (), like a
> python lambda function or so, and then called immediately. Is that what's
> happening?
>
AFAIK that is exactly what is going on, though I have to say I am also a
bit puzzled by the need of parenthesis around the anonymous function.
--
Pertti
More information about the Qt-qml
mailing list