我想检查一个对象是否已定义..
对象的内容:
所以我会这样做:
if (e.model.item.state != "undefined"){
var stateID = e.model.item.state.id;
....
}
else{
}
然后 e.model.item.state 未定义,但它确实进入 if 子句并在此停止:
var stateID = e.model.item.state.id;
因为未定义..!
我也尝试过:
!== "undefined"
!=== "undefined"
最佳答案
最好使用它来避免不必要的未定义错误
:-
if (e && e.model && e.model.item & e.model.item.state) {
// e.model.item.state is NOT `undefined`, `null`, `false` or `0`
}
关于Javascript 检查未定义是否不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38321401/