Amit Tomar Amit Tomar - 1 year ago 157
Javascript Question

Default arguments for qml function gives syntax errors

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 ?

Answer Source

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 fith edition

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download