Home  >  Q&A  >  body text

"What does mean?"

<p>I'm trying to get the value returned from a function <code>alert</code> and I get the following in the popup: </p> <pre class="brush:none;toolbar:false;">[object Object] </pre> <p>The following is the JavaScript code: </p> <pre class="brush:html;toolbar:false;"><script type="text/javascript"> $(function () { var $main = $('#main'), $1 = $('#1'), $2 = $('#2'); $2.hide(); // Hide div#2 when the page loads $main.click(function () { $1.toggle(); $2.toggle(); }); $('#senddvd').click(function () { alert('hello'); var a=whichIsVisible(); alert(whichIsVisible()); }); function whichIsVisible() { if (!$1.is(':hidden')) return $1; if (!$2.is(':hidden')) return $2; } }); </script> </pre> <p><code>whichIsVisible</code> is the function I'm trying to check. </p>
P粉358281574P粉358281574401 days ago895

reply all(2)I'll reply

  • P粉458913655

    P粉4589136552023-08-18 09:58:57

    As others have pointed out, this is the default serialization of an object. But why is it [object Object] and not just [object]?

    This is because there are different types of objects in Javascript!

    • Function Object:
      stringify(function (){}) -> [object Function]
    • Array object
      stringify([]) -> [object Array]
    • Regular Expression Object:
      stringify(/x/) -> [object RegExp]
    • Date Object:
      stringify(new Date) -> [object Date]
    • there are more
    • AlsoObject object!
      stringify({}) -> [object Object]

    This is because the constructor is called Object (capital "O"), and the term "object" (lowercase "o") refers to the structural nature of the object.

    Usually, when talking about "objects" in Javascript, you actually mean "Object objects", not other types.

    Where stringify should be like this:

    function stringify (x) {
        console.log(Object.prototype.toString.call(x));
    }

    reply
    0
  • P粉465287592

    P粉4652875922023-08-18 09:48:03

    The default result of converting an object to a string is "[object Object]".

    Since you are dealing with jQuery objects, you may want to do the following

    alert(whichIsVisible()[0].id);

    To print the ID of the element.

    As mentioned in the comments, you should use the tools included in browsers like Firefox or Chrome to inspect objects, instead of using alert, you can do console.log(whichIsVisible( )).

    Note: ID should not start with a number.

    reply
    0
  • Cancelreply