Default arguments for qml function gives syntax errors

℡╲_俬逩灬. 提交于 2020-12-30 08:59:19

问题


This code works fine in browser hosted JavaScript environment :

    function foo(a=true)
    {
        console.log(a)
    }

But doing the same in qml is giving syntax error.

What might I be doing wrong ?


回答1:


In QML, you should write the function as

function foo(a) {
    if (a === undefined) a = true

    console.log(a)
}

The syntax

function foo(a=true)

is not supported as this syntax was introduced in ECMA-262 6th edition while QML only implements the fifth edition (as of Qt 5.11).




回答2:


From Qt 5.12, this code will work fine. See release note.

  • The JavaScript engine now supports ECMAScript 7

Hope this will help people in the future.



来源:https://stackoverflow.com/questions/44128131/default-arguments-for-qml-function-gives-syntax-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!