为什么[object object]提及"object"?两次? [英] Why [object Object] mentions "object" twice?

查看:106
本文介绍了为什么[object object]提及"object"?两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[object Object]是JavaScript Object的默认字符串表示形式.

[object Object] is the default string representation of a JavaScript Object.

我想知道它只是[Object]还是[object],但是为什么[object Object]?为什么第一个单词是小写字母,第二个单词是大写字母?它是JSON还是JavaScript的一部分?

I would understand if it was just [Object] or [object], but why [object Object]? Why the first word is lowercase and the second one is uppercase? Is it part of JSON or JavaScript?

推荐答案

所有对象都有一个toString方法,该方法以[object type]格式显示内容,其中type是对象类型.

All objects have a toString method that displays something in the format [object type], where type is the object type.

null上调用时会得到[object Null],在对象上调用时会得到字符串[object Object],因为它基于object,这是对象的原因类型对象.

When called on null you get [object Null] and when called on an object you get the string [object Object] because it's based on the Object constructor (capital "O"), that's why it says object twice, it's an object of the type Object.

MDN

JSON是一种轻量级的数据交换格式,除了被命名为"JavaScript Object Notation" 之外,它实际上与JavaScript没有任何关系,因为它的写法类似于JavaScript对象.

JSON is a lightweight data-interchange format, and doesn't really have anything to do with JavaScript other than being named "JavaScript Object Notation" because it's written like a JavaScript object.

这篇关于为什么[object object]提及"object"?两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆