logo

NJP

Object Literal Enhancements - Let's Learn ECMAScript 2021 with Earl Duque

Import · Jan 25, 2024 · video

So back in 2015, ECMAScript 6 introduced
so much different things. and some of the things I'm going to be
talking about in this video is the things that they enhanced
to the object literals syntax, so things like shorthand properties
and computed property names. So if I explain shorthand property
really quick. So let's say we have two variables declared
and then we create an object. I can just simply put userId in here,
and because userId has already been declared
and set the shorthand property of ES6 automatically pulls in that value of it
because it's already been declared. And then similarly, instead of having to write out function logDetails
and then creating my function or logDetails equals function,
it understands that when you have a parameter right
here, you're trying to create a function so you don't have to write
all those words out anymore. So shorthand properties and if I press run it operated as normal
even without the additional assignment and without the actual
saying of the word function in there. So it's very straightforward. But yeah, it's the little things, right. And computed property
names is exactly that. So you can see in this object
that I've created that the property name itself is being set via
an expression. So I'm adding the word is to this variable that I've brought in
that is said to Status. And then down here
I'm looking specifically in this object for isStatus. But you can see here there's no isStatus property that's set directly. It's all via a computed property name. So if I press run still found it as true
because that property name is there via script. Cool. I see. Cool, don't I?

View original source

https://www.youtube.com/watch?v=FZxL1syRHf8