[对象对象]是什么意思?

发布于 2024-10-13 23:58:02 字数 801 浏览 2 评论 0原文

我试图警告函数返回的值,我在警报中得到了这个:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

滥情哥ㄟ 2024-10-20 23:58:02

[object Object] 是 javascript 中对象的默认 toString 表示形式。

如果您想知道对象的属性,只需像这样 foreach 即可:

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

在您的特定情况下,您将获得一个 jQuery 对象。尝试这样做:

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

这应该提醒可见元素的 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:

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

In your particular case, you are getting a jQuery object. Try doing this instead:

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

This should alert the id of the visible element.

王权女流氓 2024-10-20 23:58:02

从对象到字符串的默认转换是“[object Object]”

当您处理 jQuery 对象时,您可能想要

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

打印元素的 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

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

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 of alert.

Sidenote: IDs should not start with digits.

╄→承喏 2024-10-20 23:58:02

您可以像这样在 [object Object] 中看到值

Alert.alert(  JSON.stringify(userDate)  );

尝试像这个

    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)  );

参考

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

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

reference

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

山色无中 2024-10-20 23:58:02

正如其他人所指出的,这是对象的默认序列化。但为什么是 [object Object] 而不仅仅是 [object] 呢?

那是因为 Javascript 中有不同类型的对象!

  • 函数对象
    stringify(function (){}) ->; [对象函数]
  • 数组对象
    stringify([]) -> [对象数组]
  • RegExp 对象
    stringify(/x/) ->; [object RegExp]
  • 日期对象
    stringify(新日期) -> [对象日期]
  • ... 还有更多 ...
  • 和<强>对象对象!
    stringify({}) -> [object Object]

那是因为构造函数称为Object(大写“O”),而术语“对象”(小写“o”)指的是事物的结构性质。

通常,当您在 Javascript 中谈论“对象”时,您实际上指的是“Object 对象”,而不是其他类型。

其中 stringify 应该如下所示:

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

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!

  • Function objects:
    stringify(function (){}) -> [object Function]
  • Array objects:
    stringify([]) -> [object Array]
  • RegExp objects
    stringify(/x/) -> [object RegExp]
  • Date objects
    stringify(new Date) -> [object Date]
  • several more
  • and Object objects!
    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:

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

你是年少的欢喜 2024-10-20 23:58:02

基础知识

你可能不知道,在 JavaScript 中,每当我们与字符串、数字或布尔基元交互时,我们都会进入一个对象阴影和强制的隐藏世界。

字符串、数字、布尔值、null、未定义和符号。

JavaScript 中有 7 种基本类型:undefinednullbooleanstringnumber代码>、<代码>bigint和<代码>符号。其他一切都是对象。基本类型 booleanstringnumber 可以由它们的对象对应项包装。这些对象分别是 BooleanStringNumber 构造函数的实例。

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"

如果基元没有属性,为什么 "this is a string".length 返回一个值?

因为 JavaScript 很容易在基元和对象之间进行强制。在这种情况下,字符串值被强制转换为字符串对象,以便访问属性长度。字符串对象只使用了几分之一秒,之后它就被牺牲给垃圾收集之神——但本着电视探索节目的精神,我们将捕获这个难以捉摸的生物并保存它以供进一步分析……

为了进一步证明这一点考虑以下示例,其中我们向 String 构造函数原型添加一个新属性。

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

通过这种方式,基元可以访问由它们各自的对象构造函数定义的所有属性(包括方法)。

因此,我们看到原始类型会在需要时适当地强制转换为各自的对象对应项。

分析 toString() 方法

考虑以下代码

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"

如上所述,真正发生的是当我们在基本类型上调用 toString() 方法时,它必须先被强制转换为其对应的对象,然后才能调用该方法。
myNumber.toString() 相当于 Number.prototype.toString.call(myNumber),对于其他基本类型也类似。

但是,如果我们不将原始类型传递到其对应的对象构造函数对应的 toString() 方法中,而是强制将原始类型作为参数传递到 toString() 上,会怎么样?对象函数构造函数的方法(Object.prototype.toString.call(x))?

仔细看看 Object.prototype.toString()

根据 文档
当调用toString方法时,会执行以下步骤:

  1. 如果this值为未定义,则返回“[object Undefined]”
  2. 如果this值为null,则返回“[object Null]”
  3. 如果该值不是以上任何一个,则令 O 为调用 toObjectthis 值作为参数传递。
  4. 设 class 为 O[[Class]] 内部属性的值。
  5. 返回由三个字符串 "[object "class"]" 连接而成的字符串值。< /里>

从以下示例

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

参考文献中了解这一点:
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 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 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.

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

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() 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, 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 to Number.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 onto toString() 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:

  1. If the this value is undefined, return "[object Undefined]".
  2. If the this value is null, return "[object Null]".
  3. If this value is none of the above, Let O be the result of calling toObject passing the this value as the argument.
  4. Let class be the value of the [[Class]] internal property of O.
  5. Return the String value that is the result of concatenating 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]"

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/

你另情深 2024-10-20 23:58:02

它是该对象的 toString() 功能。


我明白你想要做什么,因为我昨天回答了你的问题 关于确定哪个 div 可见。 :)
whichIsVisible() 函数返回一个实际的 jQuery 对象,因为我认为这在编程上会更有用。如果您想使用此函数进行调试,您可以执行以下操作:

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

也就是说,您确实应该使用适当的调试器而不是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:

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

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.

木落 2024-10-20 23:58:02

[object Object] 是 JavaScript Object 的默认字符串表示形式。如果运行此代码,您将得到以下结果:

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

您可以通过覆盖 toString 方法来更改默认表示形式,如下所示:

var o = {toString: function(){ return "foo" }};
alert(o); // foo

[object Object] is the default string representation of a JavaScript Object. It is what you'll get if you run this code:

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

You can change the default representation by overriding the toString method like so:

var o = {toString: function(){ return "foo" }};
alert(o); // foo
最丧也最甜 2024-10-20 23:58:02

我认为最好的方法是使用 JSON.stringify() 并将数据作为参数传递:

alert(JSON.stringify(whichIsVisible()));

I think the best way out is by using JSON.stringify() and passing your data as param:

alert(JSON.stringify(whichIsVisible()));
心病无药医 2024-10-20 23:58:02

您有一个 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 use alert($1.text()); to get text or alert($1.attr('id'); etc...

you have to treat $1 and $2 like jQuery objects.

远昼 2024-10-20 23:58:02

考虑以下示例:

const foo = {};
foo[Symbol.toStringTag] = "bar";
console.log("" + foo);

哪些输出

[object bar]

基本上,javascript 中的任何对象都可以使用标签 Symbol.toStringTag 定义属性并覆盖输出。

在幕后,使用“toString”方法从某个对象构造 JavaScript 原型中的新对象。默认对象提供此方法作为属性,并且该方法在内部调用标记来确定如何将对象强制为字符串。如果该标签存在,则使用它,如果缺少,您将得到“Object”

您应该设置 Symbol.toStringTag 吗?或许。但是对于“真正的”对象来说,始终依赖于字符串 [object Object] 并不是最好的主意。

Consider the following example:

const foo = {};
foo[Symbol.toStringTag] = "bar";
console.log("" + foo);

Which outputs

[object bar]

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.

記柔刀 2024-10-20 23:58:02

类为 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文