[object Object]是什么意思? (JavaScript的)

时间:2022-02-06 16:56:22

One of my alerts is giving the following result:

我的一个提醒是给出以下结果:

[object Object] 

What does this mean exactly? (This was an alert of some jQuery object.)

这究竟是什么意思? (这是一些jQuery对象的警报。)

5 个解决方案

#1


28  

It means you are alerting an instance of an object. When alerting the object, toString() is called on the object, and the default implementation returns [object Object].

这意味着您正在警告对象的实例。在警告对象时,在对象上调用toString(),默认实现返回[object Object]。

var objA = {};
var objB = new Object;
var objC = {};

objC.toString = function () { return "objC" };

alert(objA); // [object Object]
alert(objB); // [object Object]
alert(objC); // objC

If you want to inspect the object, you should either console.log it, JSON.stringify() it, or enumerate over it's properties and inspect them individually using for in.

如果要检查对象,则应该是console.log它,JSON.stringify()它,或枚举它的属性,并使用for in单独检查它们。

#2


4  

The alert() function can't output an object in a read-friendly manner. Try using console.log(object) instead, and fire up your browser's console to debug.

alert()函数无法以读取友好的方式输出对象。请尝试使用console.log(object),然后启动浏览器控制台进行调试。

#3


3  

I wrote this answer in other question that was duplicated it, and a soon I want to put the answer got close, so any way here is my answer. my two cents, and hope some else help in any way.

我在其他问题中写了这个答案,这个答案是重复的,很快我就想把答案搞定了,所以这里任何方式都是我的答案。我的两分钱,并希望其他任何方面的帮助。

As @Matt answered the reason of [object object] so you have three options JSON.stringify(JSONobject), console.log(JSONobject) or iterate over the object, look the follow basic example.

由于@Matt回答了[对象对象]的原因所以你有三个选项JSON.stringify(JSONobject),console.log(JSONobject)或迭代对象,看下面的基本例子。

var jsonObj={
    property1 : "one",
    property2 : "two",
    property3 : "three",
    property4 : "fourth",
};

var strBuilder = [];
for(key in jsonObj){
      if (jsonObj.hasOwnProperty(key)) {
         strBuilder.push("Key is " + key + ", value is " + jsonObj[key] + "\n");
    }
}

alert(strBuilder.join(""));

https://jsfiddle.net/b1u6hfns/

https://jsfiddle.net/b1u6hfns/

#4


1  

Alerts aren't the best for displaying objects. Try console.log? If you still see Object Object in the console, use JSON.parse like this > var obj = JSON.parse(yourObject); console.log(obj)

警报不是显示对象的最佳选择。试试console.log?如果您仍然在控制台中看到Object Object,请使用JSON.parse,如下所示> var obj = JSON.parse(yourObject);执行console.log(OBJ)

#5


-1  

If you are popping it in the DOM then try wrapping it in

如果你在DOM中弹出它,那么尝试将其包装进去

<pre>
    <code>{JSON.stringify(REPLACE_WITH_OBJECT, null, 4)}</code>
</pre>

makes a little easier to visually parse.

使视觉解析更容易一些。

#1


28  

It means you are alerting an instance of an object. When alerting the object, toString() is called on the object, and the default implementation returns [object Object].

这意味着您正在警告对象的实例。在警告对象时,在对象上调用toString(),默认实现返回[object Object]。

var objA = {};
var objB = new Object;
var objC = {};

objC.toString = function () { return "objC" };

alert(objA); // [object Object]
alert(objB); // [object Object]
alert(objC); // objC

If you want to inspect the object, you should either console.log it, JSON.stringify() it, or enumerate over it's properties and inspect them individually using for in.

如果要检查对象,则应该是console.log它,JSON.stringify()它,或枚举它的属性,并使用for in单独检查它们。

#2


4  

The alert() function can't output an object in a read-friendly manner. Try using console.log(object) instead, and fire up your browser's console to debug.

alert()函数无法以读取友好的方式输出对象。请尝试使用console.log(object),然后启动浏览器控制台进行调试。

#3


3  

I wrote this answer in other question that was duplicated it, and a soon I want to put the answer got close, so any way here is my answer. my two cents, and hope some else help in any way.

我在其他问题中写了这个答案,这个答案是重复的,很快我就想把答案搞定了,所以这里任何方式都是我的答案。我的两分钱,并希望其他任何方面的帮助。

As @Matt answered the reason of [object object] so you have three options JSON.stringify(JSONobject), console.log(JSONobject) or iterate over the object, look the follow basic example.

由于@Matt回答了[对象对象]的原因所以你有三个选项JSON.stringify(JSONobject),console.log(JSONobject)或迭代对象,看下面的基本例子。

var jsonObj={
    property1 : "one",
    property2 : "two",
    property3 : "three",
    property4 : "fourth",
};

var strBuilder = [];
for(key in jsonObj){
      if (jsonObj.hasOwnProperty(key)) {
         strBuilder.push("Key is " + key + ", value is " + jsonObj[key] + "\n");
    }
}

alert(strBuilder.join(""));

https://jsfiddle.net/b1u6hfns/

https://jsfiddle.net/b1u6hfns/

#4


1  

Alerts aren't the best for displaying objects. Try console.log? If you still see Object Object in the console, use JSON.parse like this > var obj = JSON.parse(yourObject); console.log(obj)

警报不是显示对象的最佳选择。试试console.log?如果您仍然在控制台中看到Object Object,请使用JSON.parse,如下所示> var obj = JSON.parse(yourObject);执行console.log(OBJ)

#5


-1  

If you are popping it in the DOM then try wrapping it in

如果你在DOM中弹出它,那么尝试将其包装进去

<pre>
    <code>{JSON.stringify(REPLACE_WITH_OBJECT, null, 4)}</code>
</pre>

makes a little easier to visually parse.

使视觉解析更容易一些。