"What does this word mean?"
P粉696605833
2023-08-20 16:52:15
<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>
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!
stringify(function (){})
->[object Function]
stringify([])
->[object Array]
stringify(/x/)
->[object RegExp]
stringify(new Date)
->[object Date]
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: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
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 doingconsole.log(whichIsVisible ())
.Note: ID should not start with a number.