Check if object exists in JavaScript

前端 未结 18 2514
抹茶落季
抹茶落季 2020-12-04 05:26

How do I verify the existence of an object in JavaScript?

The following works:

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

But this throws a

相关标签:
18条回答
  • 2020-12-04 05:45

    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 , x.m , x.m[z] and typeof x.m[z] would fail with an error if x.m did not exist.

    I hope it helps. (BTW, I am novice with JS)

    0 讨论(0)
  • 2020-12-04 05:46

    Two ways.

    typeof for local variables

    You can test for a local object using 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) {}
    
    0 讨论(0)
  • 2020-12-04 05:47

    The thread was opened quite some time ago. I think in the meanwhile the usage of a ternary operator is the simplest option:

    maybeObject ? console.log(maybeObject.id) : ""
    
    0 讨论(0)
  • 2020-12-04 05:48

    You could use "typeof".

    if(typeof maybeObject != "undefined")
        alert("GOT HERE");
    
    0 讨论(0)
  • 2020-12-04 05:49
    if (n === Object(n)) {
       // code
    }
    
    0 讨论(0)
  • 2020-12-04 05:51

    set Textbox value to one frame to inline frame using div alignmnt tabbed panel. So first of all, before set the value we need check selected tabbed panels frame available or not using following codes:

    Javascript Code :

    /////////////////////////////////////////
    <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>
    
    0 讨论(0)
提交回复
热议问题