最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Default arguments for qml function gives syntax errors - Stack Overflow

matteradmin1PV0评论

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 ?

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 ?

Share Improve this question asked May 23, 2017 at 7:15 Amit TomarAmit Tomar 4,8587 gold badges53 silver badges86 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

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).

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.

Post a comment

comment list (0)

  1. No comments so far