Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript多维isset()_Javascript_Jquery - Fatal编程技术网

Javascript多维isset()

Javascript多维isset(),javascript,jquery,Javascript,Jquery,运行时: if (data.custaccount.webaddress) { alert('found it'); } 我得到了错误 data.custaccount is undefined 我能绕过它的唯一方法似乎是使用多个这样的“如果”: if (undefined != data && undefined != data.custaccount && undefined != data.custaccount.webaddress) {

运行时:

if (data.custaccount.webaddress) {
   alert('found it');
}
我得到了错误

data.custaccount is undefined
我能绕过它的唯一方法似乎是使用多个这样的“如果”:

if (undefined != data && undefined != data.custaccount && undefined != data.custaccount.webaddress) {
   alert('found it');
}
我有没有办法做得更简单一些

在php中,我们通常使用isset(data.custaccount.webaddress),这非常有效。javascript(或jquery)中是否有一个等价物

我们使用了try/catch,但发现这会大大降低脚本的性能

我曾看到其他人也在问类似的问题,但没有任何成功,但我希望stackoverflow能够完成这一天的正常工作:)

谢谢


Justin

当然,在访问
data.custcount.webaddress
之前,您需要检查
数据
是否已定义

你可以把支票写得更短一些

if(data && data.custaccount && data.custaccount.webaddress){
}
你需要检查一下

你说得对,使用
try/catch
是不好的做法。另一种查找对象的方法是使用类似于

if('custaccount' in data && 'webaddress' in data.custaccount){
}

第一种方法看起来很明显,但功能不全:如果
data.custaccount.webaddress
返回错误值(
false
0
,等等),它就不会执行它想要执行的操作。