- Overview
- Transcript
5.9 Getters and Setters
As well as regular object properties, we can also create properties that have getters and setters, which are special functions that run when the property is set or accessed.
Syntax
get
set
this
1.Introduction2 lessons, 07:42
1.1Introduction02:12
1.2Setup05:30
2.Language Fundamentals8 lessons, 1:00:53
2.1Variables06:33
2.2Data Types11:28
2.3Arithmetic, Assignment, and Comparison Operators10:24
2.4Unary, Logical, Comma, and Spread Operators09:02
2.5Operator Precedence03:50
2.6Reserved Words04:17
2.7Strict Mode04:34
2.8Functions10:45
3.Data Structures5 lessons, 22:52
3.1Arrays04:29
3.2Objects04:30
3.3Sets04:57
3.4Maps04:21
3.5Weak Maps and Weak Sets04:35
4.Controlling Program Execution7 lessons, 37:06
4.1Conditionals07:49
4.2Switch Statements04:41
4.3The For Loop06:39
4.4The `for .. in` Loop05:17
4.5The `for .. of` Loop04:02
4.6Iterators05:03
4.7While Loops03:35
5.Using JavaScript13 lessons, 1:44:36
5.1Working With Strings09:32
5.2Template Literals05:46
5.3Working With Numbers06:57
5.4Working With Arrays12:53
5.5Iterating and Transforming Arrays07:33
5.6Working With the Object Type13:55
5.7Object Literal Extensions06:45
5.8Working With Object Instances06:45
5.9Getters and Setters05:00
5.10Custom Objects11:28
5.11The `Math` API04:54
5.12Working With Dates and Times08:10
5.13The `Array` Constructor04:58
6.Functions8 lessons, 56:07
6.1The `this` Object06:15
6.2Working With Functions10:11
6.3Scope07:37
6.4Arrow Functions06:59
6.5Generator Functions08:13
6.6Closures05:00
6.7Prototypes06:26
6.8Default and Rest Parameters05:26
7.Miscellaneous6 lessons, 52:39
7.1Destructuring Assignments08:09
7.2AJAX08:30
7.3Regular Expressions10:51
7.4More About Regular Expressions08:38
7.5Classes06:48
7.6ES Modules09:43
8.Working With the DOM6 lessons, 37:39
8.1Selecting HTML Elements05:02
8.2Manipulating HTML Elements07:40
8.3DOM Traversal05:25
8.4Adding and Removing Elements04:45
8.5Creating Elements and Other Nodes04:39
8.6DOM Events10:08
9.Web APIs4 lessons, 17:41
9.1The Selector API03:03
9.2Geolocation05:29
9.3Web Storage05:24
9.4Web Workers03:45
10.Asynchronous JavaScript5 lessons, 26:23
10.1Promises09:52
10.2Promise Chaining05:11
10.3The async Keyword03:21
10.4The await Keyword04:04
10.5More About async and await03:55
11.Conclusion1 lesson, 00:43
11.1Conclusion00:43
5.9 Getters and Setters
Hi folks, in this lesson, we can take a look at adding getters and setters to properties that we create in object literals. Getters and setters are special functions that are invoked automatically whenever a property is accessed or assigned to. Let's start by looking at getters. We can add a getter using the get keyword in front of the property name. So, in this example, we create a regular object literal called, myObj. We give it a regular property with the key, otherProp, and the value, other, in string format. We then add a new property called prop, but we use the get keyword in front of the property name. And notice how the property actually looks like a method. In this example, we'll just log to the console and then return the value of the other prop property. So now, whenever we access prop, it will invoke the getter. And let's review the output in a browser. So we see our log message and then the getter property actually returns the value of otherProp, which in this case is the string, other. Inside our getter, we can perform whatever logic we want. Although, as I said, in this case, we just log a message to the console and then return a different property value. Again inside the getter, we've used the special object called, this. We will come back to this shortly in the next section, in this context, just understand that this refers to the object, myObj. So now let's take a look at setters. We can add a setter for the same property that we have a getter for. So a setter looks very much like a getter except that we use the set keyword instead of the get keyword. And a setter will receive a value, and that is the new value that is being set to the property. So in this basic example, we can just do another console log, and we'll just log out the value that we are setting. And on the last line inside the setter, this line here, we set the value of otherProp to the new value. We don't have to add both getters and setters to the same property, although we have done in this case. And when using getters and setters, we will very often set or get a different property than the getter or setter themselves. In the console.log statement down at the bottom, you can see that we were getting the value prop. But that was actually returning the value of otherProp. And this is most commonly how getters and setters are used. So let's just invoke our new setter. And let's just log out the objects once we've invoked the setter. Let's go back to the browser now, so we see the log message from the setter, and below that, we can see a log of the object. And we can see that the other property has had its value set to new. It's important to understand, however, that the other property that we've been working with is not hidden or protected in any way. Just because we're using a getter and a setter with it. We can still access otherProp directly if we want to. But if we are building something to be used by other developers, like a widget, or a plugin for some framework. We only need to publicize the property that has the getter and the setter attached to it. And not the property that the getter and setter operates on, we don't have to use a different property if we just want to add a getter. We only needed it in this example because we had both a getter and a setter. So in this lesson, we learned how to add getters and setters to properties within object literals. Getters and setters are properties that have methods which are invoked when the property is either accessed. Or has a new value assigned to it. We also learned that inside a getter or a setter, the this keyword points to the object that the getter or setter belongs to. In the next lesson, we're going to look at custom objects. Thanks for watching.