[object 对象] 是什么意思?(JavaScript)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8892465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:57:10  来源:igfitidea点击:

What does [object Object] mean? (JavaScript)

javascriptjquery

提问by Sriram

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 对象的警报。)

回答by Matt

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].

这意味着您正在提醒一个对象的实例。当alerting 对象时,在对象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.logit, JSON.stringify()it, or enumerate over it's properties and inspect them individually using for in.

如果你想检查对象,你应该是console.log它,JSON.stringify()它,或者枚举它的属性并使用for in.

回答by ncubica

As @Matt answered the reason of [object object], I will expand on how to inspect the value of the object. There are three options on top of my mind:

由于@Matt 回答了 的原因[object object],我将扩展如何检查对象的值。我脑子里有三个选项:

  • JSON.stringify(JSONobject)
  • console.log(JSONobject)
  • or iterate over the object
  • JSON.stringify(JSONobject)
  • console.log(JSONobject)
  • 或迭代对象

Basic example.

基本示例。

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(""));
// or console.log(strBuilder.join(""))

https://jsfiddle.net/b1u6hfns/

https://jsfiddle.net/b1u6hfns/

回答by jetsie

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) 代替,并启动浏览器的控制台进行调试。

回答by John Michelin

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.

使视觉解析更容易一些。

回答by Hyman1536

Another option is to use JSON.stringify(obj)

另一种选择是使用 JSON.stringify(obj)

For example:

例如:

exampleObj = {'a':1,'b':2,'c':3};
alert(JSON.stringify(exampleObj))

https://www.w3schools.com/js/js_json_stringify.asp

https://www.w3schools.com/js/js_json_stringify.asp

回答by Guillaume250

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)