This course has been updated! We now recommend you take the TypeScript Fundamentals, v3 course.
Transcript from the "Enhanced Object Literal" Lesson
[00:00:00]
>> Mike North: The next thing we're gonna talk about following the pattern of kinda going back into JavaScript looking at how the language specification has evolved. And then, connecting back to Type Script we're gonna look at object literals. So as hopefully you know Cuz that would create an object literal.
[00:00:21] We can have properties on an object literal like this. Functions are first-class values in JavaScript, so a function is a value, just like a string or a number is a value. We can use them pretty much interchangeably. There are limitations around what we can use as property keys but other than that, it's pretty much Do whatever you need to do.
[00:00:42] So this is the way things were for a while. The 2015 version of the language spec had a category of features that fit into this idea of an enhanced object literal. I wanna walk you through what's going on here. So first off, you've got the ability to specify the prototype in an object literal, right.
[00:01:05] So normally, you'd have to do this in a couple of steps. You'd have to use the new operator and then assign a bunch of properties, depending on what that object allows that might take a couple of steps to do. Here you can use double underscore specify the prototype just in the middle of an object literal.
[00:01:29] Here we've got what's called the dynamic property name. So using square brackets, you can see that company is something that maybe we know about only at run time. Maybe it's not that like it's linked in up there but What if we used prompt and asked the user for that value?
[00:01:46] So like at build time, we don't necessarily know what that is. That's why it's dynamic. So in square brackets, we can use that to have a dynamic property name here. That was a bit difficult to do in other ways. So that was difficult to do in one pass like this before we had this feature.
[00:02:06]
>> Mike North: All right, we have a shorthand for one of the variable, where value is stored and the property key where we want to store it are the same. We have a shorthand where we do not have to do company, colon, company. We, as you see on the left can just do company, and then continue on.
[00:02:25] So we don't have to the key in the value being identical their. And then, finally there's this idea of methods, this is just a short hand for just as we were doing with classes, you don't have to say two string colon function. You can just say, toString, and then ().
[00:02:45] Finally, we already talked about this, but we have super calls, not just in classes, but in object liberals as well that goes up the prototype chain just as it does under the hood in classes.