Javascript对象调查

标签 javascript object properties

从原始问题更新

我正在尝试通过查看它的属性来了解 javascript 对象“看起来像”什么:

//create object without prototype
var obj = Object.create(null);
//add properties
obj.a = 3;
obj.b = 76.3;
obj.c = { a : 23 , b : true , c : 1};
obj.s = "ABC!";
//output string
var outStr = new String();

for(var prop in obj)
{
    outStr += "Property " + prop + " is a " + typeof(obj[prop]) + " with value " + obj[prop] + "\n";

    for(var prop1 in obj[prop])
    {
        outStr += "Property " + prop1 + " is a " + typeof(obj[prop][prop1]) + " with value " + obj[prop][prop1] + "\n";
        for(var prop2 in obj[prop][prop1])
        {
            outStr += "Property " + prop2 + " is a " + typeof(obj[prop][prop1][prop2]) + " with value " + obj[prop][prop1][prop2] + "\n";
            for(var prop3 in obj[prop][prop1][prop2])
            {
                outStr += "Property " + prop3 + " is a " + typeof(obj[prop][prop1][prop2][prop3]) + " with value " + obj[prop][prop1][prop2][prop3] + "\n";
                for(var prop4 in obj[prop][prop1][prop2][prop3])
                {
                    outStr += "Property " + prop4 + " is a " + typeof(obj[prop][prop1][prop2][prop3][prop4]) + " with value " + obj[prop][prop1][prop2][prop3][prop4] + "\n";
                }
            }
        }
    }
}   

alert(outStr);

给出输出:

Property a is a number with value 3
Property b is a number with value 76.3
Property c is a object with value [object Object]
  Property a is a number with value 23
  Property b is a boolean with value true
  Property c is a number with value 1
Property s is a string with value ABC!
  Property 0 is a string with value A
  Property 0 is a string with value A
  Property 0 is a string with value A
  Property 0 is a string with value A
  Property 1 is a string with value B
  Property 0 is a string with value B
  Property 0 is a string with value B
  Property 0 is a string with value B
  Property 2 is a string with value C
  Property 0 is a string with value C
  Property 0 is a string with value C
  Property 0 is a string with value C
  Property 3 is a string with value !
  Property 0 is a string with value !
  Property 0 is a string with value !
  Property 0 is a string with value !

现在,除了 String 属性 obj.s = "ABC!"之外,每个属性的行为都完全符合我的预期;

我知道 obj.s 包含属性(键和值):

“0”=“A”

“1”=“B”

“2”=“C”

“3”=“!”

从之前的回答(非常感谢@pimvdb 和@deestan)我了解到,因为这些属性值中的每一个都是字符串,它们也都包含属性键“0”,它本身也必须包含属性键“0”等等,等等?这就是为什么我要为字符串属性写出额外的行。

所以我的问题现在变成了:

所有属性值的类型是否应该在某个时候最终恢复为原始类型(如数字或 bool 值)以停止此递归链?我正在考虑实际保存这个对象的内存,当我开始编写这个小测试脚本来“查看”对象时,我基本上想查看所有基元以及对象如何存储它们,但实际上不能字符串到字符串到字符串的无限分配...我想它只是存储为字符串对象,它有 4 个字符(如果也有字符串结尾字符\0,则为 5 个)

最佳答案

可能不是所发布问题的答案,但我认为这是对您实际问题的答案::)

安装 Chrome 并打开 Javascript 控制台 (CTRL+SHIFT+J)。您可以在那里创建对象并从 Javascript 调试器中直观地检查它们。

> x = "stringystringstring"
  "stringystringstring"
>

然后在右上角的“Watch Expressions”中添加x进行检查。

如您所见,x 作为一个字符串并不是很有趣,所以让我们让它更复杂一些:

> x = { a: "b" }
|> Object
>

刷新 Watch Expressions 并展开 x 以查看对象结构。它应该看起来像这样:

v x: Object
    a: "b"
  v __proto__: Object
    |> __defineGetter__: function __defineGetter__()...
    |> __defineSetter__: function __defineSetter__()...
    ...
    |> valueOf: function valueOf() { [native code] }

我们从中了解到对象 x 有一个属性,以及从对象原型(prototype)继承的一堆方法。


如果您需要查看可访问的所有 属性和方法,例如字符串,在控制台中输入 "derp".。半秒后,应出现一个显示所有可用属性的自动完成列表。

关于Javascript对象调查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9634249/

相关文章:

javascript - 添加属性/方法的明确最佳方式

javascript - 为什么在 Google 搜索框中输入字符时 FirBug 上什么也没有显示?

javascript - 自动更改 Javascript 和 CSS 文件路径

c++ - 无法更新作为列表中节点一部分的对象内的值

c# - 一种捕获对象属性变化的方法

python - 使用属性装饰器的Python类中的属性行为

javascript - 获取 Firestore 数据库额外数组的值

javascript - html mailto 链接中的光标位置

javascript - 我可以对此使用更高的功能吗?

javascript - 在带空格的对象内搜索字符串名称