Home  >  Q&A  >  body text

"What does this word mean?"

<p>I'm trying to pop a return value from a function and in the popup I get: </p> <pre class="brush:none;toolbar:false;">[object Object] </pre> <p>This 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粉696605833P粉696605833427 days ago383

reply all(2)I'll reply

  • P粉818317410

    P粉8183174102023-08-21 12:37:52

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

    That's 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 constructors are called Object (capital "O"), and the term "object" (lowercase "o") refers to the structural nature of things.

    Usually, when you talk about "objects" in Javascript, you actually mean "ObjectObject", not other types.

    Where stringify should be like this:

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

    reply
    0
  • P粉611456309

    P粉6114563092023-08-21 11:12:52

    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, instead of using alert, you should use tools included in browsers like Firefox or Chrome to inspect objects by doing console.log(whichIsVisible ()).

    Note: ID should not start with a number.

    reply
    0
  • Cancelreply