简体   繁体   中英

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?

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.

Now, is obname now an object? How should I view obname right now?

This leads me to the sub-second question.

How do I now access the properties of object1 via newobject ?

 var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname);

  • Targeting the obname returns [object object] . Why?
  • Targeting obname.object1 returns undefined . Why?
  • Targeting obname.object1.color returns error. Why?

I can only access the properties of object1 this way:

 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 .

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?

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? How should I view obname right now?

Technically, obname is a "property" or "key" name of the newObject object and it stores a reference to the 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?

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"

 var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname.color);

Targeting the obname returns [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.

Targeting obname.object1 returns undefined. Why?

Because object1 is not declared within obname , it's "referenced" via the obname property. Think of this as a pointer... obname points to where object1 can be found.

Targeting obname.object1.color returns error. Why?

Because obname is a pointer to object1 and object1 doesn't have a property called object1 .

You can simply think of obname is equivalent to 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.color which returns the value "red"

  • Targeting the obname returns [object object]. Why? Because obname is an Object.

Think of it as:

var newobject = { 
  "obname": { "color" : "red"}
}
  • Targeting obname.object1 returns undefined. Why?
  • Targeting obname.object1.color returns error. Why?

Because obname does not have any property called object1. Instead obname and object1 are equivalent Objects (they have one property color as defined by yourself).

alert(newobject.obname.color);

This works because you are accessing obname property correctly.

To answer the question why is color a property of obname; That is because you have assign obname to object1 so they are the same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM