问题
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