检查JavaScript中是否存在对象

本文翻译自:Check if object exists in JavaScript

How do I verify the existence of an object in JavaScript? 如何验证JavaScript中是否存在对象?

The following works: 以下作品:

if (!null)
   alert("GOT HERE");

But this throws an Error: 但这会引发错误:

if (!maybeObject)
   alert("GOT HERE");

The Error: 错误:

maybeObject is not defined. maybeObject没有定义。


#1楼

参考:https://stackoom.com/question/HZCk/检查JavaScript中是否存在对象


#2楼

if (maybeObject !== undefined)
  alert("Got here!");

#3楼

set Textbox value to one frame to inline frame using div alignmnt tabbed panel. 使用div alignmnt选项卡式面板将Textbox值设置为一帧以内联框架。 So first of all, before set the value we need check selected tabbed panels frame available or not using following codes: 因此,首先,在设置该值之前,我们需要使用以下代码检查选定的选项卡式面板框架是否可用:

Javascript Code : JavaScript代码:

/
<script>

function set_TextID()
            {
                try
                    {
                        if(!parent.frames["entry"])
                            {
                            alert("Frame object not found");    
                            }
                            else
                            {
                                var setText=document.getElementById("formx").value;
                                parent.frames["entry"].document.getElementById("form_id").value=setText;
                            }
                            if(!parent.frames["education"])
                            {
                            alert("Frame object not found");    

                            }
                            else
                            {
                                var setText=document.getElementById("formx").value;
                                parent.frames["education"].document.getElementById("form_id").value=setText;
                            }
                            if(!parent.frames["contact"])
                            {
                            alert("Frame object not found");    

                            }
                            else
                            {
                                var setText=document.getElementById("formx").value;
                                parent.frames["contact"].document.getElementById("form_id").value=setText;
                            }

                        }catch(exception){}
                }

</script>

#4楼

Two ways. 两种方式。

typeof for local variables 局部变量的typeof

You can test for a local object using typeof: 您可以使用typeof测试本地对象:

if (typeof object !== "undefined") {}

window for global variables 全局变量的窗口

You can test for a global object (one defined on the global scope) by inspecting the window object: 您可以通过检查窗口对象来测试一个全局对象(在全局范围内定义的一个对象):

if (window.FormData) {}

#5楼

Think it's easiest like this 认为这样最简单

if(myobject_or_myvar)
    alert('it exists');
else
   alert("what the hell you'll talking about");

#6楼

Apart from checking the existence of the object/variable you may want to provide a "worst case" output or at least trap it into an alert so it doesn't go unnoticed. 除了检查对象/变量的存在之外,您可能还想提供“最坏情况”输出,或者至少将其捕获到警报中,以免引起注意。

Example of function that checks, provides alternative, and catch errors. 检查,提供替代方法并捕获错误的函数示例。

function fillForm(obj) {
  try {
    var output;
    output = (typeof obj !== 'undefined') ? obj : '';
    return (output);
  } 
  catch (err) {
    // If an error was thrown, sent it as an alert
    // to help with debugging any problems
    alert(err.toString());
    // If the obj doesn't exist or it's empty 
    // I want to fill the form with ""
    return ('');
  } // catch End
} // fillForm End

I created this also because the object I was passing to it could be x , xm , xm[z] and typeof xm[z] would fail with an error if xm did not exist. 我之所以创建它也是因为我要传递给它的对象可能是x,xm,xm [z]和typeof xm [z]如果xm不存在,则会因错误而失败。

I hope it helps. 希望对您有所帮助。 (BTW, I am novice with JS) (顺便说一句,我是JS新手)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值