Var vor Deklaration verwendet

Diese Warnkategorie wird von qmllint [var-used-before-declaration] geschrieben.

Variable wird hier vor ihrer Deklaration verwendet

Was ist passiert?

Sie haben eine Variable verwendet, bevor Sie sie deklariert haben.

Warum ist das schlecht?

Das macht den Code schwieriger zu lesen. Variablen, die mit let oder const deklariert werden, führen zu Fehlern bei der Ausführung.

Beispiel

import QtQuick

Item {
    function f() {
        x = 42; // x is used before its declaration
        let x;
    }

    Component.onCompleted: f()
}

Um diese Warnung zu beheben, verschieben Sie die Deklaration vor die Verwendung:

import QtQuick

Item {
    function f() {
        let x;
        x = 42;
    }

    Component.onCompleted: f()
}

Siehe auch JavaScript-Ausdrücke in QML-Dokumenten.

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.