可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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 the use 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.
回答2:
As others have noted, this is the default serialisation of an object. But why is it [object Object]
and not just [object]
?
That is because there are different types of objects in Javascript!
- Function objects:
stringify(function (){})
-> [object Function]
- Array objects:
stringify([])
-> [object Array]
- RegExp objects
stringify(/x/)
-> [object RegExp]
- Date objects
stringify(new Date)
-> [object Date]
- … several more …
- and Object objects!
stringify({})
-> [object Object]
That\'s because the constructor function is called Object
(with a capital \"O\"), and the term \"object\" (with small \"o\") refers to the structural nature of the thingy.
Usually, when you\'re talking about \"objects\" in Javascript, you actually mean \"Object objects\", and not the other types.
where stringify
should look like this:
function stringify (x) {
console.log(Object.prototype.toString.call(x));
}
回答3:
[object Object]
is the default toString representation of an object in javascript.
If you want to know the properties of your object, just foreach over it like this:
for(var property in obj) {
alert(property + \"=\" + obj[property]);
}
In your particular case, you are getting a jQuery object. Try doing this instead:
$(\'#senddvd\').click(function ()
{
alert(\'hello\');
var a=whichIsVisible();
alert(whichIsVisible().attr(\"id\"));
});
This should alert the id of the visible element.
回答4:
It\'s the value returned by that object\'s toString()
function.
I understand what you\'re trying to do, because I answered your question yesterday about determining which div is visible. :)
The whichIsVisible()
function returns an actual jQuery object, because I thought that would be more programmatically useful. If you want to use this function for debugging purposes, you can just do something like this:
function whichIsVisible_v2()
{
if (!$1.is(\':hidden\')) return \'#1\';
if (!$2.is(\':hidden\')) return \'#2\';
}
That said, you really should be using a proper debugger rather than alert()
if you\'re trying to debug a problem. If you\'re using Firefox, Firebug is excellent. If you\'re using IE8, Safari, or Chrome, they have built-in debuggers.
回答5:
[object Object]
is the default string representation of a JavaScript Object
. It is what you\'ll get if you run this code:
alert({}); // [object Object]
You can change the default representation by overriding the toString
method like so:
var o = {toString: function(){ return \"foo\" }};
alert(o); // foo
回答6:
You can see value inside [object Object] like this
Alert.alert( JSON.stringify(userDate) );
Try like this
realm.write(() => {
const userFormData = realm.create(\'User\',{
user_email: value.username,
user_password: value.password,
});
});
const userDate = realm.objects(\'User\').filtered(\'user_email == $0\', value.username.toString(), );
Alert.alert( JSON.stringify(userDate) );
reference
https://off.tokyo/blog/react-native-object-object/
回答7:
You have a javascript object
$1
and $2
are jquery objects, maybe use alert($1.text());
to get text or alert($1.attr(\'id\');
etc...
you have to treat $1
and $2
like jQuery objects.