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
177,655 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