声明前使用的变量

此警告类别由 qmllint 拼写[var-used-before-declaration]

此处在变量声明之前使用了变量

发生了什么?

您在声明之前使用了变量。

为什么这样做不好?

这会增加代码的阅读难度。用letconst 声明的变量会在运行时出错。

示例

import QtQuick

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

    Component.onCompleted: f()
}

要修复此警告,请将声明移到使用之前:

import QtQuick

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

    Component.onCompleted: f()
}

另请参阅 QML 文档中的 JavaScript 表达式

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