对象内的javascript对象 [英] javascript object within an object

查看:69
本文介绍了对象内的javascript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个javaScript对象"top.ui.cmn.iFrame".
我使用if(top.ui.cmn.iFrame)检查iFrame属性是否为空.
但是,如果"top"为空,则上面将给出脚本错误.有没有一种优雅的方法来检查对象内的对象是否不为空而不是做
if(top && top.ui && top.ui.cmn && top.ui.cmn.iFrame){

Hi,
I have a javaScript object "top.ui.cmn.iFrame".
I use if(top.ui.cmn.iFrame) to check if iFrame property is null.
However, if "top" is null, the above will give a script error. Is there any elegant way of checking if the objects within the object are not null rather than doing
if(top && top.ui && top.ui.cmn && top.ui.cmn.iFrame){

推荐答案

您可以使用try ... catch以及/代替您的if语句:

You could use try ... catch as well as/instead of your if statement:

try {
  if(top.ui.cmn.iFrame) {
    // top.ui.cmn.iFrame true
  } else {
    // top.ui.cmn.iFrame false and top.ui.cmn exists
  }
} catch(e) {
  // top or top.ui or top.ui.cmn undefined
}


这篇关于对象内的javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆