我有一个 JavaScript 方法给我这个错误:
Cannot read property 'filter' of undefined
我一直在尝试调试这个,但运气不佳。有没有一种方法可以在此处之前检查变量的值,如果未定义,则暂停浏览器,以便我可以转到控制台并检查其他变量。
下面是涉及的代码:
this.getFilteredTestStatusActions = function (screen, roleName) {
var self = _this;
var filtered = self.userTestStatusActions.filter(function (val) {
我的问题是 self.userTestStatusActions 未定义。如果浏览器未定义,我想在此时暂停浏览器,以便我可以“查看”其他变量。
最佳答案
通常,您可以通过执行以下操作来检查 JavaScript 中的变量是否未定义
:
if(self.userTestStatusActions === undefined){
// self.userTestStatusActionshas not been defined
}
您可以使用 debugger;
关键字强制使用 JavaScript 调试器。
function testUndefined(myVariable){
if(myVariable === undefined){
debugger;
}
}
testUndefined()
关于javascript - 如果变量未定义,有没有办法可以停止浏览器中的脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26652812/