"What does this word mean?"
P粉696605833
P粉696605833 2023-08-20 16:52:15
0
2
387
<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粉696605833
P粉696605833

reply all(2)
P粉818317410

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));
}

P粉611456309

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!