第一种情况,明明定义了一个XXX属性,却提示Cannot read property ‘XXX’ of undefined;
TypeError: Cannot read property ‘XXX’ of undefined。
第二种情况,使用this.setData报错:thirdScriptError, this.setData not a funtion。
第一种解决方法:吧普通函数
function (res) {
...
}
变成箭头函数
(res) => {
},因为箭头函数内部是有没this的绑定,函数里面的this就是函数外部的this。
第二种方法:function(res){
//在普通函数的开头添加
var _this = this;
},来绑定this
本文介绍了两种常见的JavaScript错误:无法读取未定义属性和使用this.setData时发生的错误,并提供了两种解决方法,包括将普通函数转换为箭头函数及手动绑定this。

3万+

被折叠的 条评论
为什么被折叠?



