From the course: Learning ECMAScript 6+ (ES6+)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Getting and setting class values

Getting and setting class values - JavaScript Tutorial

From the course: Learning ECMAScript 6+ (ES6+)

Start my 1-month free trial

Getting and setting class values

- [Instructor] In objects and in classes, JavaScript has a get method and a set method. A getter or a get method gets the value of a specific property. So if you're wondering, what is this property for this object? The getter is going to go get it for you. The setter does what it sounds like. It sets the value of a specific property. So we can use getters and setters in objects and in classes. Let's take a look at how this looks in an object by creating an object called attendance. The first key of attendance we'll use is _list, which will be an empty array. And then we're going to add this set property here and set will be a function called addName. Which will just push an item into the list. Then finally we'll create a getter, a get property. And this is going to return the elements of the list array joined together with a comma. Once we have our object created, let's go ahead and use it. On line 11, we're going…

Contents