Details
-
Bug
-
Resolution: Fixed
-
P2: Important
-
5.15.1
-
None
Description
Relevant code inside main.qml:
[0].map(index => { console.assert(this, "Totally fine"); { console.assert(this, "Not fine"); } });
Actual output:
Not fine (qrc:/main.qml:8) onCompleted (qrc:/main.qml:5)
Expected output: Empty
Opening a block inside a closure inside map seems to make this undefined. That is inconsistent with other JS engines. Loops and the lambda without map do not show this behavior.
The other files outside main.qml are for project setup.
You can also test it online with the code
import QtQuick 2.7 import QtQuick.Controls 2.3 Text { text: { return [0].map(index => { if (this) { if (this) { return "Level 2"; } return "Level 1"; } return "Level 0"; }).join(); } }
at https://qmlonline.kde.org which should display "Level 2" but displays "Level 1" instead.
Workaround:
Put
const self = this;
at the start of the closure block and then use self instead of this.