问题:
This question already has an answer here: 这个问题已经在这里有了答案:
- Detecting an undefined object property 42 answers 检测未定义的对象属性 42个答案
- How to check a not-defined variable in JavaScript 14 answers 如何检查JavaScript中未定义的变量 14答案
- How to handle 'undefined' in javascript [duplicate] 3 answers 如何处理javascript中的“ undefined” [重复] 3个答案
- how to check if a variable exist in javascript? 如何检查JavaScript中是否存在变量? 8 answers 8个答案
What is the most appropriate way to test if a variable is undefined in JavaScript? 测试JavaScript中是否未定义变量的最合适方法是什么? I've seen several possible ways: 我已经看到了几种可能的方法:
if (window.myVariable)
Or 要么
if (typeof(myVariable) != "undefined")
Or 要么
if (myVariable) //This throws an error if undefined. Should this be in Try/Catch?
解决方案:
参考一: https://stackoom.com/question/EDzo/如何在JavaScript中检查-未定义-重复参考二: https://oldbug.net/q/EDzo/How-to-check-for-undefined-in-JavaScript-duplicate
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/3212560