Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can't understand how object interacted with another object?

Hell, I am having trouble understading how the object interacted with another object, could someone please explain to me what just happened in this video? the video is this one:

https://teamtreehouse.com/library/object-interaction, could someone give me a general breakdown of what the code did? one thing that really confuses me is the way the teacher writes the same name over and over in this code, I am not even sure which instance of owner the code would refer to later on, if owner is repeated several times

set owner(owner) { //setter example this._owner = owner; console.log(setter called: ${owner}`

Steven Parker
Steven Parker
243,656 Points

About what time index in the video?

1 Answer

Steven Parker
Steven Parker
243,656 Points

I found it in a different video, but I'm still not sure what you meant by "interacted with another object", since there's only one object in this example. This method is created (and explained) starting about 1 minute into the Setters video. And the name "owner" is not a reference to the instance.

But I can see how it might be a bit confusing. Let's look at one line at a time:

  set owner(owner) {

The first "owner" is the property name, the second one is the parameter.

    this._owner = owner; 

Here, "this" is the instance, "_owner" (notice the underscore) is the backing field, and "owner" is once again the parameter.

If you didn't care about the conventions used in the course, you might write it this way:

  set owner(parameter) {
    this.backing_field_for_owner = parameter; 

Does that clear it up?

do we always have to have backing fields in getters and setters?

Steven Parker
Steven Parker
243,656 Points

Normally, yes. But you might not for some cases. For example, a getter might return a computed property made from one or more other fields:

    get fullName() { return `${this.firstName} ${this.lastName}`; }

ok, so let me see if I understood most of this unit:

-object literals are like arrays in the sense that we can organize our data more effectively and we can store more than one variable right?

  • we use Class along with constructor to create more 'object literals' with similar properties to avoid writing a lot of code right?
Steven Parker
Steven Parker
243,656 Points

An object literal is where you create an object directly with the data in it. It would not use "class" or "constructor". Here's an example of one:

let person = { name: "Joe", age: 22 };  // assign "person" using an object literal

But we've wandered off the original question topic a bit.

Steven Parker
Steven Parker
243,656 Points

Erik L — Was your orignal question answerd? You can mark it solved by choosing a "best answer".

And happy coding!