What does the [object Object] mean?

I try to warn the return value from the function and I get this in the message

[object of 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 a function I'm trying to test against

+88
javascript jquery
Jan 20 2018-11-17T00:
source share
9 answers

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

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

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

print item id.

As mentioned in the comments, you should use the tools included with browsers such as Firefox or Chrome to parse objects by executing console.log(whichIsVisible()) instead of alert .

Sidenote: Identifiers should not begin with numbers.

+40
Jan 20 '11 at 17:09
source share

As others have already noted, 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!

  • Functional Objects:
    stringify(function(){}) β†’ [object Function]
  • Array of objects:
    stringify([]) β†’ [object Array]
  • RegExp objects
    stringify(/x/) β†’ [object RegExp]
  • Date objects
    stringify(new Date) β†’ [object Date]
  • ... a few more ...
  • and objects objects !
    stringify({}) β†’ [object Object]

This is because the constructor function is called Object (with a capital β€œO”), and the term β€œobject” (with a capital β€œo”) refers to the structural nature of a thing.

Usually, when you talk about β€œobjects” in Javascript, you really mean β€œ objects of objects, ” not other types.

where stringify should look like this:

 function stringify (x) { console.log(Object.prototype.toString.call(x)); } 

+102
Aug 21 '14 at 6:25
source share

[object Object] is the default view of the toString of an object in javascript.

If you want to know the properties of your object, just do it like this:

 for(var property in obj) { alert(property + "=" + obj[property]); } 

In your specific case, you get a jQuery object. Try to do this instead:

 $('#senddvd').click(function () { alert('hello'); var a=whichIsVisible(); alert(whichIsVisible().attr("id")); }); 

This should warn the identifier of the visible element.

+19
Jan 20 '11 at 17:09
source share

This is the value returned by this toString() object.




I understand what you are trying to do, because yesterday I answered your question about determining which div is visible. :)
The whichIsVisible() function returns the actual jQuery object, because I thought it would be more programmatically useful. If you want to use this function for debugging purposes, you can simply do something like this:

 function whichIsVisible_v2() { if (!$1.is(':hidden')) return '#1'; if (!$2.is(':hidden')) return '#2'; } 

However, you really should use the correct debugger , not alert() if you are trying to debug the problem. If you use Firefox, Firebug is great. If you use IE8, Safari or Chrome, they have built-in debuggers.

+11
Jan 20 '11 at 17:09
source share

You can see the value inside the [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) ); 

link

https://off.tokyo/blog/react-native-object-object/

+6
Aug 01 '18 at 16:32
source share

[object Object] is the standard string representation of JavaScript Object . This is what you get if you run this code:

 alert({}); // [object Object] 

You can change the default view by overriding the toString method as follows:

 var o = {toString: function(){ return "foo" }}; alert(o); // foo 
+4
Jan 20 '11 at 17:10
source share

The basics

You may not know this, but in JavaScript, whenever we interact with string, numeric, or logical primitives, we find ourselves in a hidden world of shadows and casts.

string, number, boolean, zero, undefined value and character.

There are 7 primitive types in JavaScript: undefined , null , boolean , string , number , bigint and symbol . Everything else is an object. The primitive types boolean , string and number can be wrapped by their object counterparts. These objects are instances of the Boolean , String and Number constructors, respectively.

 typeof true; //"boolean" typeof new Boolean(true); //"object" typeof "this is a string"; //"string" typeof new String("this is a string"); //"object" typeof 123; //"number" typeof new Number(123); //"object" 

If primitives have no properties, why does "this is a string".length return a value?

Because JavaScript will easily cast between primitives and objects. In this case, the string value is cast to the string object to access the length of the property. A string object is used only for a split second, after which it is sacrificed to the gods of garbage collection - but in the spirit of television discoveries, we will catch an elusive creature and save it for further analysis ...

To demonstrate this further, consider the following example, in which we add a new property to the prototype of the String constructor.

 String.prototype.sampleProperty = 5; var str = "this is a string"; str.sampleProperty; // 5 

Thus, primitives have access to all properties (including methods) defined by their respective object constructors.

So, we saw that primitive types will accordingly lead to the corresponding object analogue if necessary.

Parsing the toString() Method

Consider the following code

 var myObj = {lhs: 3, rhs: 2}; var myFunc = function(){} var myString = "This is a sample String"; var myNumber = 4; var myArray = [2, 3, 5]; myObj.toString(); // "[object Object]" myFunc.toString(); // "function(){}" myString.toString(); // "This is a sample String" myNumber.toString(); // "4" myArray.toString(); // "2,3,5" 

As discussed above, in reality, when we call the toString() method for a primitive type, it must be cast to its object analog before it can call the method.
i.e. myNumber.toString() equivalent to Number.prototype.toString.call(myNumber) and similarly for other primitive types.

But what if, instead of passing the primitive type to the toString() method of the corresponding function-analogue of the constructor of the Object, we force the primitive type to be passed as a parameter to the toString() method of the Object's constructor method ( Object.prototype.toString.call(x) )?

Take a closer look at Object.prototype.toString ()

According to the documentation , When the toString method is called, the following steps are taken:

  1. If this is undefined , return "[object Undefined]" .
  2. If this is null , return "[object Null]" .
  3. If this value is not specified above, let O be the result of calling toObject with passing the value of this as an argument.
  4. Let class be the value of the [[Class]] internal property of O
  5. Returns a String value that is the result of combining the three strings "[object " , class and "]" .

Understand this from the following example.

 var myObj = {lhs: 3, rhs: 2}; var myFunc = function(){} var myString = "This is a sample String"; var myNumber = 4; var myArray = [2, 3, 5]; var myUndefined = undefined; var myNull = null; Object.prototype.toString.call(myObj); // "[object Object]" Object.prototype.toString.call(myFunc); // "[object Function]" Object.prototype.toString.call(myString); // "[object String]" Object.prototype.toString.call(myNumber); // "[object Number]" Object.prototype.toString.call(myArray); // "[object Array]" Object.prototype.toString.call(myUndefined); // "[object Undefined]" Object.prototype.toString.call(myNull); // "[object Null]" 

Links: https://es5.imtqy.com/x15.2.html#x15.2.4.2 https://es5.imtqy.com/x9.html#x9.9 https://javascriptweblog.wordpress.com/ 2010/09/27 / the-secret-life-of-javascript-primitives /

+3
Aug 01 '19 at 13:27
source share

Do you have a javascript object

$1 and $2 are jquery objects, maybe use alert($1.text()); to receive text or alert($1.attr('id'); etc ...

you need to treat $1 and $2 objects as jQuery objects.

+2
Jan 20 '11 at 17:11
source share

You are trying to return an object. Since there is no good way to represent an object as a string, the value of the .toString() object is automatically set to "[object Object]" .

0
Jun 14 '19 at 14:18
source share



All Articles