javascript - QML javascript:如果在箭头函数中,“this”在 if(){...} 下的类构造函数中变得未定义
问题描述
在 QML javascript 中,当执行纯 javascript 类构造函数时,“this”在下面的代码中暂时未定义,if (cond) { ... }
但在其他方面应该是有效的。
仅当代码位于匿名箭头函数的主体中时,才会出现此问题(似乎)。JS 箭头函数不提供“this”或对它做任何事情,所以符号this
应该引用它在封闭范围内的定义,即类构造函数;它是,除了条件if (c) {...}
!
这有什么可以解释的?
import QtQuick 2.14
Item {
Component.onCompleted: {
class Bugtest {
constructor() {
this.foo = 123;
((arg) => {
print("BEFORE if: this="+this+", foo="+this.foo);
if (true) {
print("DURING if: this="+this); //undefined here only!!
}
print("AFTER if: this="+this+", foo="+this.foo);
})(42);
print("At constructor exit: this="+this+", foo="+this.foo);
}
};
var xyz = new Bugtest();
}
}
使用qml(或qmlscene )v6.2.1 或 v5.15.2运行上述内容会产生:
qml: BEFORE if: this=[object Object], foo=123
qml: DURING if: this=undefined
qml: AFTER if: this=[object Object], foo=123
qml: At constructor exit: this=[object Object], foo=123
解决方案
这看起来确实像一个 QML 错误,其中“this”绑定的范围被破坏了。虽然我无法解释为什么会这样,但我找到了一种解决方法,您.bind(this)
在输入函数之前明确表示,这会导致if
条件的内容正确评估:
import QtQuick 2.15
Item {
Component.onCompleted: {
class Bugtest {
constructor() {
this.foo = 123;
((arg) => {
console.log("BEFORE if: this="+this+", foo="+this.foo);
if (true) {
console.log("DURING if: this="+this+", foo="+this.foo); // now works in QML and JSFiddle
}
console.log("AFTER if: this="+this+", foo="+this.foo);
}).bind(this)(42); // added explicit 'this' bind here
console.log("At constructor exit: this="+this+", foo="+this.foo);
}
};
var xyz = new Bugtest();
}
}
推荐阅读
- c++ - 将 C++ 和 C/C++ 静态库与 CMake 混合
- ios - iOS 13 apple-app-site-association 通配符用于包含/排除而没有部分路径
- single-sign-on - 如何使用“itfoxtec-identity-saml2”项目在 Umbraco v8(ADFS - 无 Azure)中创建自定义 SSO 成员登录?
- google-apps-script - 修改 Google 图表 - 在我通过脚本(显示日期)动态更新图表标题后,垂直轴“数字格式”恢复为“来自源数据”
- javascript - 如何根据数据库表信息显示传单标记?
- python - Python多处理队列子类在进程中丢失属性
- javascript - Js无法识别从异步调用返回的数组
- r - 在 ggplot 上创建小方块
- amcharts - 如何将周数读入 amCharts?
- javascript - 如何从此 javascript 承诺中正确返回值