Javascript访问父变量的正确方法

标签 javascript qt qml

Qt 在 https://doc.qt.io/qt-5/qtquick-performance.html 中指定以下代码。 cumulatedValue 变量作为“root.accumulatedValue”和“accumulatedValue”进行访问。只是想了解在这种情况下访问父变量的正确方法是什么。

// good.qml
import QtQuick 2.3

Item {
    id: root
    width: 200
    height: 200
    property int accumulatedValue: 0

    Text {
        anchors.fill: parent
        text: root.accumulatedValue.toString()
        onTextChanged: console.log("text binding re-evaluated")
    }

    Component.onCompleted: {
        var someData = [ 1, 2, 3, 4, 5, 20 ];
        var temp = accumulatedValue;
        for (var i = 0; i < someData.length; ++i) {
            temp = temp + someData[i];
        }
        accumulatedValue = temp;
    }
}

最佳答案

当从同一个 scope 中访问属性时作为其所属的对象,不需要使用 id 来限定访问。从不同范围访问该属性时,您应该使用该属性所属对象的 id 对其进行限定。

使用qmllint举一些例子:

import QtQuick 2.3

Item {
    id: root
    width: 200
    height: 200
    property int accumulatedValue: 0

    Text {
        /*
            Warning: unqualified access at /tmp/good.qml:20:21

                    objectName: accumulatedValue
                                ^^^^^^^^^^^^^^^^
            Note: accumulatedValue is a member of the root element
                  You can qualify the access with its id to avoid this warning:

                    objectName: root.accumulatedValue
        */
        objectName: accumulatedValue
        anchors.fill: parent
        // Fine; qualified with id.
        text: root.accumulatedValue

        Component.onCompleted: {
            /*
                Warning: unqualified access at /tmp/good.qml:36:19

                            print(accumulatedValue)
                                  ^^^^^^^^^^^^^^^^
                Note: accumulatedValue is a member of the root element
                      You can qualify the access with its id to avoid this warning:

                            print(root.accumulatedValue)
            */
            print(accumulatedValue)
        }
    }

    Component.onCompleted: {
        var someData = [ 1, 2, 3, 4, 5, 20 ];
        // Fine; in same scope.
        var temp = accumulatedValue;
        for (var i = 0; i < someData.length; ++i) {
            temp = temp + someData[i];
        }
        accumulatedValue = temp;
    }
}

不合格的访问代价高昂,因为它们需要引擎搜索各种范围才能找到属性。

另一个原因是可读性,在上面链接的范围文档中进行了解释:

Dynamic scoping is very powerful, but it must be used cautiously to prevent the behavior of QML code from becoming difficult to predict. In general it should only be used in cases where the two components are already tightly coupled in another way. When building reusable components, it is preferable to use property interfaces [...]

关于Javascript访问父变量的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66452939/

相关文章:

c++ - QQuickPaintedItem 使用 QPainter 缓慢更新

user-interface - 如何创建 Qt 快速测试

javascript - 如何在react对象中使用if else条件

javascript - 无法让我的 bootstrap carousel 工作 - jQuery 行禁用其余代码

c++ - Qt QSignalMapper 不工作

c++ - 动态链接时 OpenCL 崩溃?

javascript - 如何将附加元素保留在父元素中?

javascript - windows 8 hover to slide effect 使用css和js

c++ - 在Qt中使用glDrawBuffer和glReadBuffer函数

menu - 如何在QML中设置弹出菜单位置