[对象对象]是什么意思?
我试图警告
函数返回的值,我在警报中得到了这个:
[object Object]
这是JavaScript代码:
<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
是我试图检查的函数。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
[object Object]
是 javascript 中对象的默认 toString 表示形式。如果您想知道对象的属性,只需像这样 foreach 即可:
在您的特定情况下,您将获得一个 jQuery 对象。尝试这样做:
这应该提醒可见元素的 id。
[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:
In your particular case, you are getting a jQuery object. Try doing this instead:
This should alert the id of the visible element.
从对象到字符串的默认转换是
“[object Object]”
。当您处理 jQuery 对象时,您可能想要
打印元素的 ID。
正如评论中提到的,您应该使用 Firefox 或 Chrome 等浏览器中包含的工具通过执行
console.log(whichIsVisible())
而不是alert
来内省对象。旁注:ID 不应以数字开头。
The default conversion from an object to string is
"[object Object]"
.As you are dealing with jQuery objects, you might want to do
to print the element's ID.
As mentioned in the comments, you should use the tools included in browsers like Firefox or Chrome to introspect objects by doing
console.log(whichIsVisible())
instead ofalert
.Sidenote: IDs should not start with digits.
您可以像这样在 [object Object] 中看到值
尝试像这个
参考
https://off .tokyo/blog/react-native-object-object/
You can see value inside [object Object] like this
Try like this
reference
https://off.tokyo/blog/react-native-object-object/
正如其他人所指出的,这是对象的默认序列化。但为什么是
[object Object]
而不仅仅是[object]
呢?那是因为 Javascript 中有不同类型的对象!
stringify(function (){})
->;[对象函数]
stringify([])
->[对象数组]
stringify(/x/)
->;[object RegExp]
stringify(新日期)
->[对象日期]
stringify({})
->[object Object]
那是因为构造函数称为
Object
(大写“O”),而术语“对象”(小写“o”)指的是事物的结构性质。通常,当您在 Javascript 中谈论“对象”时,您实际上指的是“Object 对象”,而不是其他类型。
其中
stringify
应该如下所示: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!
stringify(function (){})
->[object Function]
stringify([])
->[object Array]
stringify(/x/)
->[object RegExp]
stringify(new Date)
->[object Date]
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:基础知识
你可能不知道,在 JavaScript 中,每当我们与字符串、数字或布尔基元交互时,我们都会进入一个对象阴影和强制的隐藏世界。
字符串、数字、布尔值、null、未定义和符号。
JavaScript 中有 7 种基本类型:
undefined
、null
、boolean
、string
、number
代码>、<代码>bigint和<代码>符号。其他一切都是对象。基本类型boolean
、string
和number
可以由它们的对象对应项包装。这些对象分别是Boolean
、String
和Number
构造函数的实例。如果基元没有属性,为什么
"this is a string".length
返回一个值?因为 JavaScript 很容易在基元和对象之间进行强制。在这种情况下,字符串值被强制转换为字符串对象,以便访问属性长度。字符串对象只使用了几分之一秒,之后它就被牺牲给垃圾收集之神——但本着电视探索节目的精神,我们将捕获这个难以捉摸的生物并保存它以供进一步分析……
为了进一步证明这一点考虑以下示例,其中我们向 String 构造函数原型添加一个新属性。
通过这种方式,基元可以访问由它们各自的对象构造函数定义的所有属性(包括方法)。
因此,我们看到原始类型会在需要时适当地强制转换为各自的对象对应项。
分析
toString()
方法考虑以下代码
如上所述,真正发生的是当我们在基本类型上调用
toString()
方法时,它必须先被强制转换为其对应的对象,然后才能调用该方法。即
myNumber.toString()
相当于Number.prototype.toString.call(myNumber)
,对于其他基本类型也类似。但是,如果我们不将原始类型传递到其对应的对象构造函数对应的
toString()
方法中,而是强制将原始类型作为参数传递到toString()
上,会怎么样?对象函数构造函数的方法(Object.prototype.toString.call(x)
)?仔细看看 Object.prototype.toString()
根据 文档,
当调用toString方法时,会执行以下步骤:
从以下示例
参考文献中了解这一点:
https://es5.github.io/x15.2.html#x15 .2.4.2
https://es5.github.io/x9.html#x9.9
https://javascriptweblog.wordpress.com/ 2010/09/27/javascript-primitives的秘密生活/
Basics
You may not know it but, in JavaScript, whenever we interact with string, number or boolean primitives we enter a hidden world of object shadows and coercion.
string, number, boolean, null, undefined, and symbol.
In JavaScript there are 7 primitive types:
undefined
,null
,boolean
,string
,number
,bigint
andsymbol
. Everything else is an object. The primitive typesboolean
,string
andnumber
can be wrapped by their object counterparts. These objects are instances of theBoolean
,String
andNumber
constructors respectively.If primitives have no properties, why does
"this is a string".length
return a value?Because JavaScript will readily coerce between primitives and objects. In this case the string value is coerced to a string object in order to access the property length. The string object is only used for a fraction of second after which it is sacrificed to the Gods of garbage collection – but in the spirit of the TV discovery shows, we will trap the elusive creature and preserve it for further analysis…
To demonstrate this further consider the following example in which we are adding a new property to String constructor prototype.
By this means primitives have access to all the properties (including methods) defined by their respective object constructors.
So we saw that primitive types will appropriately coerce to their respective Object counterpart when required.
Analysis of
toString()
methodConsider the following code
As discussed above, what's really happening is when we call
toString()
method on a primitive type, it has to be coerced into its object counterpart before it can invoke the method.i.e.
myNumber.toString()
is equivalent toNumber.prototype.toString.call(myNumber)
and similarly for other primitive types.But what if instead of primitive type being passed into
toString()
method of its corresponding Object constructor function counterpart, we force the primitive type to be passed as parameter ontotoString()
method of Object function constructor (Object.prototype.toString.call(x)
)?Closer look at Object.prototype.toString()
As per the documentation,
When the toString method is called, the following steps are taken:
Understand this from the following example
References:
https://es5.github.io/x15.2.html#x15.2.4.2
https://es5.github.io/x9.html#x9.9
https://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/
它是该对象的
toString()
功能。我明白你想要做什么,因为我昨天回答了你的问题 关于确定哪个 div 可见。 :)
whichIsVisible()
函数返回一个实际的 jQuery 对象,因为我认为这在编程上会更有用。如果您想使用此函数进行调试,您可以执行以下操作:也就是说,您确实应该使用适当的调试器而不是
alert()
,如果你正在尝试调试一个问题。如果您使用的是 Firefox,Firebug 非常适合。如果您使用 IE8、Safari 或 Chrome,它们都有内置调试器。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: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.[object Object]
是 JavaScriptObject
的默认字符串表示形式。如果运行此代码,您将得到以下结果:您可以通过覆盖
toString
方法来更改默认表示形式,如下所示:[object Object]
is the default string representation of a JavaScriptObject
. It is what you'll get if you run this code:You can change the default representation by overriding the
toString
method like so:我认为最好的方法是使用 JSON.stringify() 并将数据作为参数传递:
I think the best way out is by using
JSON.stringify()
and passing your data as param:您有一个 javascript 对象
$1
和$2
是 jquery 对象,可以使用alert($1.text());
获取文本或alert($1.attr('id');
等...您必须像 jQuery 对象一样对待
$1
和$2
。You have a javascript object
$1
and$2
are jquery objects, maybe usealert($1.text());
to get text oralert($1.attr('id');
etc...you have to treat
$1
and$2
like jQuery objects.考虑以下示例:
哪些输出
基本上,javascript 中的任何对象都可以使用标签
Symbol.toStringTag
定义属性并覆盖输出。在幕后,使用“toString”方法从某个对象构造 JavaScript 原型中的新对象。默认对象提供此方法作为属性,并且该方法在内部调用标记来确定如何将对象强制为字符串。如果该标签存在,则使用它,如果缺少,您将得到
“Object”
。您应该设置
Symbol.toStringTag
吗?或许。但是对于“真正的”对象来说,始终依赖于字符串[object Object]
并不是最好的主意。Consider the following example:
Which outputs
Basically, any object in javascript can define a property with the tag
Symbol.toStringTag
and override the output.Behind the scenes construction of a new object in javascript prototypes from some object with a "toString" method. The default object provides this method as a property, and that method internally invokes the tag to determine how to coerce the object to a string. If the tag is present, then it's used, if missing you get
"Object"
.Should you set
Symbol.toStringTag
? Maybe. But relying on the string always being[object Object]
for "true" objects is not the best idea.类为 Object 的对象看起来与通常的类实例对象有很大不同,因为它的行为就像关联数组或列表:它可以通过简单的对象文字(键和属性的列表)创建,如下所示: obj={A:'a',B:'b'}; 并且因为当它显示在开发人员工具控制台窗格中以及转换为 JSON 字符串时,它看起来非常像相同的文字表示法。
但事实上,其他类的对象(从 Object 派生或扩展)的唯一真正区别是,除了属性(变量)之外,其他类通常还具有构造函数和方法(这些都是函数)。类实例对象是使用“new”运算符分配的,其属性和方法可通过“this”变量访问。您还可以使用“prototype”属性访问复制到每个新实例的底层静态函数,甚至可以通过向其原型对象添加新函数来扩展系统类。
Array 对象也派生自 Object,并且经常使用:它是一个有序的、从 0 索引的变量值数组。
与数组和其他类不同,对象对象被简单地视为关联数组(有时被认为是有序的,有时被认为是无序的)。
The object whose class is Object seems quite different from the usual class instance object, because it acts like an associative array or list: it can be created by simple object literals (a list of keys and properties), like this:
let obj={A:'a',B:'b'};
and because it looks very like this same literal notation when displayed in the Developer Tools Console pane and when it is converted to a JSON string.But, in fact, the only real difference in objects of other classes (which are derived or extended from Object) is that other classes usually have constructors and methods (these are all functions), in addition to properties (which are variables). A class instance object is allocated using the 'new' operator, and its properties and methods are accessible through the 'this' variable. You can also access the underlying static functions that are copied to each new instance by using the 'prototype' property, and even extend system classes by adding new functions to their prototype object.
The Array object is also derived from Object and is frequently used: it is an ordered, 0-indexed array of variable values.
Object objects, unlike Arrays and other classes are treated simply as associative arrays (sometimes considered ordered, and sometimes considered unordered).