Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

javascript - What does [object Object] mean?

I am trying to alert a returned value from a function and I get this in the alert:

[object Object]  

Here is the JavaScript code:

<script type="text/javascript">
$(function ()
{
var $main = $('#main'),
    $1 = $('#1'),
    $2 = $('#2');

$2.hide(); // hide div#2 when the page is loaded

$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>

whichIsVisible is the function which I am trying to check on.

question from:https://stackoverflow.com/questions/66066820/why-does-putting-x-in-a-template-string-change-the-output

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The default conversion from an object to string is "[object Object]".

As you are dealing with jQuery objects, you might want to do

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

to print the element's ID.

As mentioned in the comments, you should use the tools included in browsers like Firefox or Chrome to introspect objects by doing console.log(whichIsVisible()) instead of alert.

Sidenote: IDs should not start with digits.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...