Man*_*anu 5 javascript undefined javascript-objects
我必须检查对象是否未定义,但是当我这样做时
typeof myUnexistingObject.myUnexistingValue == 'undefined'
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
Uncaught ReferenceError: myUnexistingObject is not defined
Run Code Online (Sandbox Code Playgroud)
那么,如何检查未定义的对象或属性?
在使用每个可能定义的属性之前,您必须检查它:
function checkUnexistingObject(myUnexistingObject) {
if (myUnexistingObject !== undefined) {
if (myUnexistingObject.otherObject !== undefined) {
console.log("All is well");
}
}
}
checkUnexistingObject({});
checkUnexistingObject({otherObject: "hey"});Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7031 次 |
| 最近记录: |