Languages

JavaScript - access object property within the object

0 points
Asked by:
Cassia
1265

How can I access the property of object within object?

I have the following object:

I wanted to set place_of_residence to country value but it didn't work. I also tried using this.country unsuccessfully.

1 answer
0 points
Answered by:
Admin Dirask Community
4380

1. Using getter (ES6+)

Since ES6 you can use get syntax to bind object property to a function that will be called when you try to access the property.

2. Alternative solution

You can refer to the object property after the object is created.

Note:

In both cases you can alternativelu use dot property accessor like: user.place_of_residence = user.country

 

References

  1. getter - JavaScript | MDN
  2. Property accessors - JavaScript | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join