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 Introducing ES2015 Classes Getter and Setter Methods

Set method, indexing.

In the example code: set nameAge (input) { let nameAge = input.split(''); this.name = nameAge[1]; this.age = nameAge[0]; }

The index values, I thought they suppose to set the position of how the return is gonna show, I have tried different things, but I'm not getting what those indexes are doing.

Any help,? thanks

2 Answers

Steven Parker
Steven Parker
231,248 Points

I didn't see this particular example in the video. And this code isn't likely to be useful because by splitting the input using an empty string argument, the resulting array will contain individual characters. So both assignments would give a single character to the properties being assigned.

The example I saw in the video split a full name using a space as the argument to split. This would produce an array which contained the first name in element 0, and the last name in element 1. These indices were then used to assign the appropriate properties with the correct name.

Thanks Steven