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 - JavaScript Tutorial
From the course: Learning ECMAScript 6+ (ES6+)
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Enhancing object literals2m 8s
-
(Locked)
Creating objects with the spread operator1m 25s
-
(Locked)
Destructuring objects3m 2s
-
(Locked)
Iterating with the for/of loop2m 40s
-
(Locked)
Introducing classes2m 28s
-
(Locked)
Inheritance with JavaScript classes1m 39s
-
(Locked)
Getting and setting class values3m 35s
-
(Locked)
-
-
-