Javascript 未捕获类型错误:无法读取属性';隐藏&x27;空的
这是我的密码Javascript 未捕获类型错误:无法读取属性';隐藏&x27;空的,javascript,Javascript,这是我的密码 var ReportFolderListUtil = { confirmBox2:null, showConfirmation: function(reportFolderId){ console.log(reportFolderId); this.confirmBox2 = Lobibox.confirm({ delay:false, title : 'Confirmation',
var ReportFolderListUtil = {
confirmBox2:null,
showConfirmation: function(reportFolderId){
console.log(reportFolderId);
this.confirmBox2 = Lobibox.confirm({
delay:false,
title : 'Confirmation',
msg : "Please take note that reports created under this folder will also be deleted. Are you sure you want to delete this folder?",
buttons: {
yes: {
'class': 'btn btn-success',
closeOnClick: false
},
no: {
'class': 'btn btn-default',
closeOnClick: true
}
},
callback: function ($this, type, ev) {
if (type === 'yes')
window.location.href = "/report/remove-folder/"+reportFolderId+".html";
ReportFolderListUtil.hideConfirmation();
JobUtil.showLoading("deleteReport","deleting folder.");
}
}
})
},
hideConfirmation: function(){
this.confirmBox2.hide();
}
我的控制台上不断出现以下错误:
我可以理解您的代码,问题是您在调用
ReportFolderListUtil.hideConfirmation
方法之前调用了ReportFolderListUtil.showConfirmation
方法,因此,ReportFolderListUtil.confirmBox2
不会用this.confirmBox2=Lobibox.confirm
重新初始化showConfirmation
方法体中的null
每个ReportFolderListUtil
变量声明
根据您的需要,您可以
a) 以如下方式保护hideConfirmation
hideConfirmation: function() {
this.confirmBox2 && this.confirmBox2.hide();
}
b) 或定义隐藏方法存根
var ReportFolderListUtil = {
confirmBox2: {
hide: function () { }
},
c) 或者查看方法调用的顺序,因为在显示之前不调用hide…confirbox2将不会有值,除非您在此上下文中执行“showConfirmation”。此外,您在回调中的“if”上缺少一个花括号 在执行函数时,记住对象的状态很重要。 为了防止出现致命错误,我建议进行更多的空检查
if(confirmbox2){//execute request}
在调用showConfirmation
之前,您可能正在调用hideConfirmation
?您可以在调用hide
之前添加检查。e、 g.if(this.confirmBox2)this.confirbox2.hide()代码>