[英]When I assign an object to a property of an object, does the property become an object? If so, how do I access the first object via the second one?
I have divided this question into several sub-questions, because they are all related, and make up the pieces to what I do not understand.我把这个问题分成了几个子问题,因为它们都是相关的,并且对我不明白的部分进行了补充。
First and foremost, when I assign an object to a property, does that property become an object too?首先,当我将 object 分配给属性时,该属性是否也变为 object?
Example:例子:
var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 }
Here, I instantiate an object object1
, and then I instantiate another object, newobject
, and assign the first object, object1
as a value to the property obname
of the newobject
object. Here, I instantiate an object object1
, and then I instantiate another object, newobject
, and assign the first object, object1
as a value to the property obname
of the newobject
object.
Now, is obname
now an object?现在, obname
现在是 object 吗? How should I view obname
right now?我现在应该如何查看obname
?
This leads me to the sub-second question.这将我引向了第二个问题。
How do I now access the properties of object1
via newobject
?我现在如何通过newobject
访问object1
的属性?
var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname);
obname
returns [object object]
.以obname
为目标返回[object object]
。 Why?为什么?obname.object1
returns undefined
.定位obname.object1
返回undefined
。 Why?为什么?obname.object1.color
returns error.定位obname.object1.color
返回错误。 Why?为什么? I can only access the properties of object1
this way:我只能通过这种方式访问object1
的属性:
var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname.color);
..by only targeting obname
, and then the property of the object1
. ..仅针对obname
,然后是object1
的属性。
The last important sub-question: Why does this work?最后一个重要的子问题:为什么会这样?
color
is not a property of obname
, so why am I able to access it this way? color
不是obname
的属性,那么为什么我可以通过这种方式访问它?
You've asked several questions here, so I'll try to hit them all....你在这里问了几个问题,所以我会试着把它们都打一遍......
var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 }
Now, is
obname
now an object?现在,obname
现在是 object 吗? How should I viewobname
right now?我现在应该如何查看obname
?
Technically, obname
is a "property" or "key" name of the newObject
object and it stores a reference to the object1
object.从技术上讲, obname
是newObject
object 的“属性”或“键”名称,它存储对object1
object 的引用。
Next:下一个:
var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname.color);
How do I now access the properties of object1 via newobject?我现在如何通过 newobject 访问 object1 的属性?
The same way to do of any other object, but you will have to access the property of the first object that stores a reference to the second one, so you'll use two levels of "dot notation"对任何其他 object 执行相同的方法,但您必须访问第一个 object 的属性,该属性存储对第二个的引用,因此您将使用两个级别的“点表示法”
var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname.color);
Targeting the
obname
returns[object object]
.以obname
为目标返回[object object]
。 Why?为什么?
Because you've attempted to "print" (via an alert
) an entire object and that's what you get when you try to treat an entire object as a string.因为您尝试“打印”(通过alert
)整个 object ,这就是您尝试将整个 object 视为字符串时得到的结果。
Targeting obname.object1 returns undefined.以 obname.object1 为目标返回未定义。 Why?为什么?
Because object1
is not declared within obname
, it's "referenced" via the obname
property.因为object1
没有在obname
中声明,所以它是通过obname
属性“引用”的。 Think of this as a pointer... obname
points to where object1
can be found.将此视为一个指针... obname
指向可以找到object1
的位置。
Targeting obname.object1.color returns error.定位 obname.object1.color 会返回错误。 Why?为什么?
Because obname
is a pointer to object1
and object1
doesn't have a property called object1
.因为obname
是指向object1
的指针,而object1
没有名为object1
的属性。
You can simply think of obname is equivalent to object1 (It is an Object).你可以简单的认为obname等价于object1(It is an Object)。
As you do access object1 properties (In this case property is color) You should be getting the value of the property as following:当您访问 object1 属性时(在这种情况下,属性是颜色)您应该获得如下属性的值:
object1.color which returns the value "red" object1.color 返回值“red”
Think of it as:把它想象成:
var newobject = {
"obname": { "color" : "red"}
}
Because obname does not have any property called object1.因为 obname 没有任何名为 object1 的属性。 Instead obname and object1 are equivalent Objects (they have one property color as defined by yourself).相反, obname 和 object1 是等效的对象(它们具有您自己定义的一种属性颜色)。
alert(newobject.obname.color);
This works because you are accessing obname property correctly.这是有效的,因为您正在正确访问 obname 属性。
To answer the question why is color a property of obname;回答为什么颜色是 obname 的属性的问题; That is because you have assign obname to object1 so they are the same那是因为您已将 obname 分配给 object1 所以它们是相同的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.