"What does mean?"
P粉358281574
2023-08-17 10:48:39
<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>
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!
stringify(function (){})
->[object Function]
stringify([])
->[object Array]
stringify(/x/)
->[object RegExp]
stringify(new Date)
->[object Date]
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: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, you should use the tools included in browsers like Firefox or Chrome to inspect objects, instead of using
alert
, you can doconsole.log(whichIsVisible( ))
.Note: ID should not start with a number.