- Overview
- Transcript
5.7 Object Literal Extensions
Recent versions of JavaScript have introduced some new tools that we can make use of when creating object literals, including shortcuts for defining properties and methods, dynamic keys, and the new assign
method.
Syntax
assign
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.7 Object Literal Extensions
Hi, folks, in this lesson we're going to look at some cool things we can do when defining object literals. One feature is a shorter syntax for defining object properties that we saw when we looked at objects earlier on in the course. Let's look at that first, instead of specifically setting properties and values, we can simply supply the value. And the key will be created for us automatically. Let's take a look, so let's start out by defining a couple of variables. So later, we might want to create a new object literal which contains these values. In previous versions of JavaScript, we would've had to do something like this. So we've defined an object literal called obj. And we want it to have two properties, one with the key x and one with the key y. And we want to give the x key the value of the x variable, and the y key the value of the y variable. So in this case, it's not too bad, because the key names and variable names are quite short. But there is a lot of duplication there. And if we were adding more variables, and we wanted to use longer key names, and we had different variable names, there could be a lot more repetition. So in the latest version of JavaScript, we can shorten that to just do this instead. When the key name is the same as the variable name, we can just supply that once and JavaScript will figure it out for us. And let's just log it to the console to ensure that it's working in the way that we expect. And we can see that our object has been created, and it has x and y keys. And it has the values of the x and y variables, as we would expect. So we've saved ourselves a bit of repetitive typing, which can be useful. It seems like a small thing, but in regular day to day JavaScript, it can save you a lot of repetition. And that's really why this feature was introduced. Another time saving and repetition saving addition is a new way of defining methods. So, in earlier versions of JavaScript, if we wanted to define a method, which is simply a function attached to the proxy of an object, we would have had to have done something like this. So, in this case, we've defined a new method on our objects. And this method is stored in a property simply called method. And we have to use the whole function keyword. So if we were adding a lot of methods we'd have to repeat that function keyword over and over again. Well, in the latest version of JavaScript, we can get rid of that function keyword all together. And that allows us to do this instead. So we simply add the parenthesis after the method. And we don't have to worry about a function keyword at all. So, again, in this case, it hasn't really saved us much time or effort. But if we were adding lots and lots of methods to this object, it would save us a considerable number of keystrokes. And it just looks a bit nicer, and it's a bit easier to read. One other new feature that we have with objects is having computed property names. So in this case we use square brackets to say that we want the key name to be a combination of the x and the y variables concatenated together. And let's just look at the output in the browser. And we can see that our object now has a key called test42, which is the output of computing x plus y. Again, a small thing, but it can be very useful. Before we finish up for the day, let's just take a quick look at another static method that we can make use of from the built in object constructor that we looked at in the last lesson. This is the assign method, and we can use it to assign the properties of one or more objects to another object, a bit like jQuery's clone method. Again, this is largely provided as a syntactic convenience to avoid having to copy the properties of objects to new objects manually. So let's take a quick recap of what we've done here. We've created a new object called composedObj. And we've used the objects.assign method, this method takes three parameters in this example. This method takes three arguments in this example. The first argument is an empty object. So this is the target object, this is the object that the method will return. The second and third arguments are the objects that we'd like to merge together. So we're merging our obj example objects and then a new object literal that just has a single property called someOtherProp. And let's just log out the value of the composed object to the console. And we can see that this new object is a combination of both our existing example object and the new object literal that we passed as the third argument. If the target object already contains a property that one of the other objects contains, it will be overwritten with a property from the last object that we passed. So in this lesson we looked at some mostly syntactic improvements to object literals which were added in ES2015. Including the shorthand for assignment, where when the key name and the variable that we'd like to add to that key are the same, we can just provide one of those. We don't have to duplicate them. And we looked at computed properties. We finished up by taking a look at the assign method, which can be used to merge two objects together to form a new object. In the next lesson, we're going to move on to look at object instances. Thanks for watching.