We’re just about four months away from the release of ES2021, the latest annual update to the ECMAScript standard, and it will bring with it quite a few quality of life improvements for JavaScript. This week we dive into just what you can expect in the update, and how you can apply the changes to gain some new advantages in your projects.
Followup Resources
Stop using PX (4:22)
ES2021 Additions (12:21)
- 4 New Exciting Features Coming in ECMAScript 2021
- 5 Upcoming JavaScript ES2021 (ES 12) Features To Watch Out for This Year
- ES2021 / ES12 New Features
- MDN Web Docs: Falsy
- New Features in ES2021
- New JavaScript Features You Can Expect in ES2021
- Nullish Coalescing Operator (??) In JavaScript – What Is It And How To Use It?
- The purpose of private methods and when to use them
Transcript
The following is a machine-generated transcript of this episode. It will contain errors until it has been reviewed and edited, and we apologize for the difficulty that may cause for screen readers. Do you want to help us speed up our transcribing process? Consider sponsoring an episode.
Hey, everybody. Thanks for joining us. This is the Drunken UX Podcast. This is episode number 83. we’re gonna be talking about how to get your JavaScript ready for ES2021. Ladies and gentlemen, thanks for sitting here with us for the next hour. I am your host, Michael Fienen.
I’m your other other host. Aaron. How you doing, Michael?
Doing well doing doing nice. It’s gonna be a good month. It’s going to be a good rest of the year. I’m I’m I’m enthusiastic about it. To the point that, uh I’m convinced that if you’re as happy as I am and you’re enjoying the drunken ex podcast. You should run by responses over a new cloud. Check about at new cloud dot com slash drunken new exits in you cloud dot com slash drunken new X. Check out what they’ve got to offer an interactive mapping illustrations there back in CMS platform and all that stuff. If you want to find us, check us out.
You know you can get us on social media over at Twitter or Facebook slash drunken ux instagram at slash drunken UX podcast or in our chatroom on discord drunken ux dot com slash discord. Hey, it’s the end of February actually is at the end of February. I think. I think it’s technically, uh, the first. Actually, it’s the first of March today. So, um, I’m a little late on that
because you’re listening to this live
graduate listening to this when you
when you hit pause will still be live. But we will patiently wait for you to resume.
It’s it’s amazing how that works. We actually have a little light in front of us. The problem is when multiple people pause at the same time. No, no,
we’re pre recorded for everyone else. But you, you listener, are the only person were alive for.
Do you have to go with this evening? I have
some telling whiskey. This was another recommendation from our previous guest, Joel Goodman and also my local friends. Marc Ambinder. Yeah, it’s Irish whiskey. Um, it’s pretty good.
That’s the one that comes in the like the clay pot, isn’t it?
Uh, no, it’s well, I mean, it’s in like a weird like a tin. It’s in a, um like, you know, like you normally the scotch cylinders you get when you buy a spinal scotch. It looks like one of those, uh, but it’s not scotches, Irish whiskey,
Tullamore dew. That’s what I’m thinking of. Tullamore Dew comes in like a little like a jug, like in like an old school like jug band. Like that’s that’s what it comes in. Um, t I was thinking t I’m doing something I said I was gonna do at the start of the season. Um, and I put it off, and then I wasn’t feeling like heavy scotch or anything like that. But I’m around. It’s time broke out the Glenfiddich Grand cru. It is a 23 year aged Scotch in American and European oak finished in the casks. Boy, this is a thing. It’s got kind of on the nose.
It’s a little bit little artificial banana, A little cedar little, uh, maybe not a sandalwood, but like an oiled like walnut stock kind of smell, little bit of cider like an apple cider, uh, smell to it on the nose. It’s got a very bready taste. Yeasty kind of taste to it. Vanilla. It’s real good. Nice. Let’s talk to get things started about in the article that Nick Guard wrote over ux design dot c c. He says that it’s time we say goodbye, pixel units.
Party means units because, like, I feel like we really need pixels just in general. Well,
I mean everything has pixels. Yes, for now. Eventually, who knows what the future holds? And arguably, we don’t have pixels anymore. You know, we still talk about the pixels, but monitors a pixel has a defined with
right one pixel with Well,
yes,
it’s like kind of foot like isn’t exactly a foot. It’s like some people’s foot.
Okay, that’s not entirely a bad metaphor. A pixel is usually regarded as about a quarter of a millimeter like that’s. But the thing is the pixel density of screens and the reason why like retina screens are just retina. Yeah, we can. Actually, a physical pixel is now much smaller than a quarter millimeter. And so, you know, browsers and in turn, monitors can actually render things smaller than a pixel. Um, but the problem with pixels is they are based on a physical measurement, and that becomes problematic for a number of reasons.
Um, Nick gets into many of these. He talks about accessibility. He talks about responsive design, Um, and how this impacts things, especially when you fix the size of something, or if you’re using the wrong kind of responsive measurements off those pixels, things won’t scale properly. Um, that’s all absolutely true. Um, I am a firm believer in the REM.
That’s the root root oriented M. Yes. Okay,
So whereas an M is relative to your parent element, only a ram is relative to your root definition. So if you say well, my base font is 16 pixels, then one ram is 16 pixels. Two rams is 32 pixels. That makes it really easy to make a responsive design scales real elegantly, um, with your site so that if somebody is zooming your page to make it more readable, they’re looking on it at it on a mobile device versus their computer versus a laptop screen. Or they’ve got the browser at 50%. REM allows things to scale very nicely in those environments.
So what? Just my own reminding with Rams. The difference between Rams and a regular M is that since the REM is relative to the root with of a root em then and with Normal is relative to the kid’s parents,
yeah, so think of that. That’s why that’s why would
be variable
if you say my route font size of 16 pixels and then in the header are in the body. Let’s use the body in the body. My foot sizes, one ram body text to 16 pixels. Cool. I want to block. Quote to be to ram. That’s 32 pixels, right? I want my site to be one M. Well, that’s 32 pixels.
Site C I T.
Yeah, C I t.
And then if you had a strong tag within that site and you said you wanted to be 1.5 m, then that would be 1.5 times 32 pixels if it’s inside the block quote. Right. Right. Okay. I remember we talked about this one. Instead
of being 24 pistols, it would be 40 pixels. No, no, that’s not even right. So even I’m getting to be 40 eight.
We talked about this with the U S. Wds thing because they did there. Route font pixel size was and pixels, right?
Was it that that’s right? It was pretty low, wasn’t it?
It was 10 pixels. Because you do things with rooms where you would say, like, if you want,
it makes the math super easy. Yeah, if you
want. If you want a 16 pixel font, then you say it’s, you know, 1.6 rooms.
Um, like, and and now I didn’t think about it when you brought up the U. S. Web design system before being based on a 10 pixel base. Now that I realize Oh, yeah, I know that just makes all your rem math super easy. Um, I’m going to do that. I’m literally as we are recording this, I am midship in in a redesigned for the drunken UX website. I’m rewriting the theme from the ground up from scratch. Um, and I was going, you know, I was going to do my normal thing. And basically, I want everything 16 pixels, obviously.
Um, but now that I think about that, I want to do it often just to see how it affects everything. Um, once I start doing my math, then because I do like that idea that Yeah, but now if I look because one thing I can’t do is when I look at some SAS and I see you know 3.625 rands, right? How much is that? How? Yeah. Like how many pixels is that then at a base size? I don’t know, But 4.8 rams. If my bases 10, I know it’s 48 pixels. I just know that, like, that’s incredibly useful. He’s got some good examples in here, mixed in like and I like he was talking about clamped font sizes.
We talked about this recently about Min Max and Clamp as CSS functions, Um, and he’s like, Well, you know, should we use pixels inside of clams and he’s like. No, there’s literally no reason to do that. And he’s right, Um, and he’s got a really good example of one where he does a clamp of 10.75 Rams with a max of 1.375 Rams. But his middle measurement is 0.5 rams plus eight view port width. MM, which is great because it means. And let’s use the tin right as your your base size. So that means your base sizes, either 7.5 pixels or 13.75 pixels, which is obviously still too small.
But for the sake of this math, it’ll work. Um, but then, through that middle, you’re saying it’s five pixels plus eight view port. You know, 8% of your view port with that’s okay, eight means 8%. So whatever the width of your Vieux port is on your device, it’s five pixels plus 8% of that. Okay, so, and that means as you scale, that middle range will scale up and down elegantly until it hits the minimum or the maximum clamp values. It’s like the it’s the perfect elastic thought at that point. Boom. You’ve got this perfectly stretchy scalable thought. Um, great example. Mhm.
Run. He’s over. Ux design dot cc. Um, nic card is the one who wrote This takes about four minutes to read it, and we’ll have a link in the show notes to go. Give it a read and think about you know how you’re using pixels and relative units in your design. Okay, so today we are going to be talking about ES2021. This is ES2021 or ES12. Those are interchangeable, though They’re they’re both correct and accurate. Mm. 12th edition.
Previous versions were things like you had just just javascript. And then you had the CMA script, and then everybody kind of forgot about that for a while, accepting tourists, and then you had es six and everybody was gaga about the S six. And then everybody kind of got about huge. I mean,
Essex arguably changed the entire future of JavaScript, period, but it had been four years since there was any kind of release. And it had been, uh, God, six years since a meaningful release at that point. So, um, Acma is a company international. Is this company that’s out there and they have put out a standard. They put out a standard that is now updated every year. Right. So we started with Yeah. Yes, six is the one that comes up a lot. Because in 2015, this big release happened, right?
It was a huge update after, you know, six years of having no meaningful updates to to the spec they realized, Hey, we need to catch up. That JavaScript is getting left behind were getting things like Jake weary and stuff like this that is trying to fill all of these gaps because javascript can’t do all of these different things. So Emma caught up and said, Hey, every June, we’re just going to release, you know, a new a new edition that will have new features, new updates and new things in it.
Okay, uh, they they have a suspect called Sigma 262 which is the defines the way a programming language should operate. Right. Okay. And that’s sort of how we derive our JavaScript. Um, Xmas script is a thing that exists, and JavaScript says cool. We’re going to implement that it is designed to conform to the ECMO script standard that enough history lesson? I think so. So what we’ve got is 2021 coming out. And so there’s a few things. There’s a lot of things that are gonna be in it that are different.
Change refined, improved. All of that, Um, one that caught me off guard. Real surprising to me was they’ve added, Replace all. There’s there’s now a string method for replace all
graduations.
This is funny to me because I didn’t realize there wasn’t a replace all like it’s one of those things that I’ve taken for granted in other languages. And I just don’t think about a lot in JavaScript, I guess. And I was like that. I saw, you know, several the articles were like, Oh, yeah, you got replaced all now and I’m like we didn’t always have replaced all what we had was replace. Obviously, there is a replace function, and you can do a global replace with it.
It’s just that you have to use rejects a regular expression to do it, and when you write your regular expression, you include the backslash G global fly, and that tells it to replace it everywhere. The thing is, a lot of people don’t know rejects real well.
That’s a shame. I
read personally
regular, as frustrating as shit. And like it is, if you don’t, if you aren’t a big fan of puzzles super annoying when you’re first learning it. But man, after, Oh, how long has it been? Probably. And at least 10 to 15 years of using them like I’m okay. I haven’t studied them fully And definitely not an expert or a master like some people I’ve seen. But they are fun. Like it’s it’s fun to try to, like, really define like, what? Is this string actually like, what? Really, what are the components here? Um, so
I I see this as a move that basically just is designed to bring some parody with other languages, which every language has a replace all function. The other thing is, um, I don’t know this for a fact, but I would bet that when it comes down to especially large scale like long page replacements, replace all is probably going to be more performant than having to, like initialize the rejects engine in in javascript.
Uh, I can’t prove that I don’t know for a fact, for all I know, replace all is using rejects under the hood. And it wouldn’t matter one way or the other. But my instinct is that it probably is faster. We’ll see.
I mean, if they’re if they’re planning for it to be a real place, all that maybe they can optimize for it. Yeah, underneath or something. You see that?
The next one is, uh, promise dot Any.
So you’re gonna have to like, this is one of the concepts of javascript that I haven’t quite boy grok yet. So I needed to explain what is promise What? What? Promise?
What? What do promise what to promise. What does this thing? Um, so you’re you’re familiar with asynchronous programming?
Yeah. Like why people use javascript in the first place,
right? Yeah. What a promise does is a promise is a function that there is a component of a function that says, Hey, I’m going to go do something. It may take a little while, but I promise you I’m gonna return something to you. Either a value or an error, but I’m going to send something back. This is the super common implementation of this is with API type calls. If you have to call an A P. I especially a third party a p I. You don’t necessarily have control over how quick that round trip will happen for that payload.
So your code can, depending on what you’re doing, you may say, you know what? I want this to just run. And meanwhile, let the rest of my code do other stuff. Or you may say, You know what? I’m going to make this function asynchronous. But when I get to that promise to that call, I wanted to do what we call a weight exactly what it sounds like. It’s a pause. It’s a breakpoint. It just tells your Java script. Hey, I got to go do something Hang tight. Don’t don’t move on without me. And so it will sit there and wait for whatever that promise is going to do.
The promise is an agreement that promises a contract in your code that says I’m coming back. And so it lets you do callbacks. Um, so what does all this mean and how does it relate to promise dot Any. So when you’re writing javascript synchronous or asynchronous, and you’re doing things that require these outside calls, normally you’re making one call and you’re waiting on that one thing that happened and you, Either it works or it doesn’t. It’s, you know, you have a try catch. And if it catches an error, then it you know, you do whatever you need to do.
Dot N e is similar to another function for promises called All settled. What that means is, you can say, Let’s say you have three things that need to happen. Let’s say you’re you’re making API calls to an image service like flicker, and you have to you have to push the image, then you want to get the metadata and then you want to, uh, increment, you know, a number on an account, right? Uh, and you need all those things to happen. But you don’t want to do anything until all three of those things are done. You could chain you could do one, and then dot Then that makes
it like synchronous, though, but
that’s yeah, that makes all of those calls synchronous. You want it all happen at once because you want it to be faster. So you know, if
you were if you were to do that now you have to do, like a variable to hold each return response and then, like a I don’t even know, like a meta promise to check when all the variables have values.
Well, what you would do now is use this all settled function, you could say all settled, promised one promise to promise three. So here’s the thing. All settled. Says I’m gonna make these three calls and I’m gonna wait for all of them to be done, regardless of how they come out. They succeed or they fail, but it will wait for all of them to be done. And then you can move on and do whatever you want. There is another one called race also kind of like it sounds.
You give it your three functions and you say, and if you call them in a dot race and what it is is it will continue once the first one comes back. Um, that is settled or rejected. What any does is it says I’m not looking for the first one. Regardless, I’m looking for the first one that succeeds. So if you have three functions and the second one is the fastest one and it comes back and it succeeds. Then it continues at that point, okay? Or it waits for all of them to fail, and it throws an aggregate error. Oh,
right, I got Joe.
So I got you. It’s basically filling in the gap, all settled, which is available now. That’s in the standard. Already. All settled means I need all of my promises to either succeed or fail or some mix race. My wishes must be fulfilled. Race means I need the first one. I don’t care if it succeeded or failed. Any says I want the first one that succeeds or I want to know none of them succeeded.
So any is a race. But with that additional condition of
if they all don’t, it’s yeah, it’s a race to the finish line. Whereas race is race to fall over the hurdles first, I don’t Mhm. Okay, so
because
race doesn’t care, race just says, Hey, I got a I got a return back from your first promise, but it failed, but right, I got my response. So that was the first one to get back to me. And so I’m just gonna go on now.
What happens if all the things in race fail.
It doesn’t matter because it will act on the first failure if the other to fail. It doesn’t care because it’s already done at that.
So so race takes the first outcome. First out
success,
success or failure? Yes, whereas any is the first one to succeed or or all fail failure. If everyone fails
and all settled is well done, regardless of success or failure. Interesting. It’s a little. It’s a little wonky like once you get it locked in, it’s pretty easy to remember. It’s kind of a pretty special use case like this is the kind of thing you’re using in a really dynamic Web app or something like, Yeah,
it’s not intuitive, but I don’t just like it.
It’s the kind of thing that when you need it, you’re gonna be glad you have it. Certainly,
I I know. I know that I knock on JavaScript a lot and I’m never taking anything. I say back about it ever. But I will say that all the a secretive stuff they do is pretty damn cool. And, um, I know this is like for embedded systems, like with no Js and stuff, but like even on the Web, though, like the asynchronous stuff is pretty neat.
It’s definitely getting more feature. Complete is the way I would look at it like it’s It’s to the point where these things are are robust enough and mature enough that you really can start doing a lot of flow control with the with your asynchronous code. Uh, for a normal Web page, you’re probably never going to use that, Uh, and that’s going to be true for some of these other ones. Like you just said, Uh, a lot of this starts to get into app building embedded at building things like that, Um, one that’s just super cosmetic.
You get numeric separators now. Mm, That’s nothing more than an underscore.
Oh, yeah, I repeat, is that
if you need to write, you know 100 trillion, you can do 100 underscore. Zero underscores.
It’s basically like a comment like it’s like an in line comment Inside of the number, you’re just you’re just separating the digits, but it’s meaningless. Otherwise,
it’s like a comma, you know, or something like that. Like normally you just type in numbers straight up. This is purely cosmetic. It doesn’t affect anything. It doesn’t change the value of the number in any way just makes it more readable. Um, honestly, I kind of dig it like that’s one of those things. I don’t do a lot. I don’t do a lot of math based stuff like that, but it’s certainly like if I’m setting constants for something. Imagine you’re doing something with pie.
No, the way I use it a lot is when you’re doing time. I’m math. We have to count seconds and you want to know, like, you know, seconds in a
time, Stuff like that,
Yeah, because you’re dealing with numbers and the billions and billions. And so having having the stuff chunked off when, especially with a bunch of zeros in a row, it’s really handy.
Yeah, and that’s the thing right? When you have a lot of zeros, a lot of nines, anything with like a string of numbers, you know, just they blur together after about a million. So
it’s it’s weird the first few times you do it, but like it’s worth it, I recommend
internationalization has a couple new features. One is list format. This one’s pretty simple. Mhm. You know, I could maybe see it, especially for logging or something like that. What it is is it lets you join an array, uh, into a string in a local safe manner. So if you have an array 123 and you use a list format to join it, you can make it say 12 and three. If you do it as a conjunction, if you do it as a disjunction, it would be 12 or three. If you did it in Spanish, it would be 12 e three.
Okay, All right. So when I in Ruby and I think you can do this in PHP also load. But when you have, like, a collection of something and you want it to be comma separated, but you don’t want to have a trailing comma, Easiest way to do that is to like exploded into an array and then input it back with commas separating the values because it’s easier to do it that way than to, like, do all this logic around, ensuring there’s no trailing comma chomping it or whatever. Um, so I guess I could see how this would be useful for that.
Yeah, you could do it if you if you did it as style. Short and type unit.
I got a grammar nerd question for you, though. Does it have a setting? We’re enabling or disabling the Oxford comma? No,
I legitimately don’t know the answer to that, so I wonder if
it has an opinion. I’m assuming that it’s a comma, has a terminal cop or the ultimate comma, followed by the conjunction and then the final element. But that would be funny if they someone didn’t want to use an Oxford comma.
I didn’t see it in the signature for the methods so
but
it’s a fair question. It really is a fair question. I wanted to go to the kid every vote. I was told I was not prepared for that question. I think it’s a fair one, though, Uh, similarly, we’ve got dates, style and time style now, which lets you pass a value to it for short, medium or long and so like short time style would be. You’re our minute am PM Long time style would be hour, minute, second am PM, and Zulu offset GMT. Offset. Okay. Uh huh. Um, date style Short is month, day, year slashes numbers. 10.
You know, I’d be like, what was, uh, 31 2021 Long style would be march 1st 2021 and similarly internationalized. And all of that. So right. And of course I mean, you can format a date in those ways, but this is annually, right
with formatting string,
and this gives you just the way of doing it short, medium long with the internationalization. So take those as you may again not going to use them a ton, probably, but they’re nice, especially if you do build stuff that is international sensitive. So use them. If they’re they’re certainly so let’s talk programming. So let’s get a little, little denser. One of the big changes is that now classes support private methods and access, sirs.
Okay. Like like traditional visibility or
so there. There. There are three types of methods, right? Yeah, normally in in a class. Right now. If you wrote a class in javascript, um, and build your constructor and got all that stuff done, every function that you add to it is inherently a public function, which which just means that you can call that function from any object reference So if Okay, if you have a function called add and it adds 1 to 1 of your properties, then you can call ad, you know, in your normal ex dot add. You know, if you if X is an instance of your object, X dot ad will always add one.
But maybe you don’t want people doing that. You know, maybe you’re trying to do internal manipulation of something you know for other purposes. You don’t want all of those things available. That’s so when we talk about private, usually in most languages, a private method is one that is only accessible inside of that class, so it’s used by like other functions. So maybe you have, add and add as a public function.
But add calls a private math function or something like that that you’ve created, and you don’t want that exposed. All you all that has to worry about is that the object uses. I don’t know. How does that compare to something like Ruby
with Ruby? It’s weird. So, like by default, methods are public, just like in JavaScript, you can do protected and you can do private. The difference is that private methods can’t call directly from the object itself via the dot thing. So if I have, like an object called Foo and I have a private method called like but I can’t do food on. But however you can indirectly call it by using the send method, which is public and then passing it.
The reference like the name of the private method you want to call, and then the object will call that private method internally, the food send and then but as a symbol, and then it would. It would let you call it so it’s not. The visibility of a method is kind of like a like a social contract that the maintainers of the class have with anyone using the class where it says this is private. If you choose to use this, you’re going out of your way to use it. Joke’s on you if we ever change it. But it’s there.
A lot of Rubio’s like that, actually, So it seems like Javascript is going a more traditional route.
There’s a really good article over at code with Jason dot com that I’m gonna have linked. I love the way he, because he has that same opinion the way he defines, like why you would want to use private methods in object oriented code. I agree with totally, and I love how he breaks it down. One is to minimize an objects ap um, to sort of reduce the surface area of an object. So maybe your object has 20 methods in it, but only two of those need to actually be public. The only two of those are things that people should interface with.
The others are all used internally to do things you know on the object, or transform data or get information or whatever. Um, so it lets you minimize that surface area so that when it comes time to document it for, you know, public stuff, you don’t need to document all those other things for people. They only need to know about the two public methods that are in it, and you don’t have to worry about them calling the other things inadvertently. The other part is that it makes your code safer to re factor.
A public method has a signature to it, and if you change that signature or you do something that public method, you risk breaking everything that calls that public method. This is really dangerous. If you make a library of some kind and want to make it available, changing signatures is a big deal for that kind of stuff. Private methods, you know, are only called and referenced within your object. So if you need to re factor, anything that uses it is already right in front of you.
And so you can, you know, change that if you need to. Um, Now I use the word their signature. What? What’s the signature? Aaron, What does that mean to you,
uh, be like to make sure it would include, like, the method name and then kind of like the argument list. And that really depends on if you’re doing static type or duck type language. But in in Ruby, it’s typically like, um, the signature would be the method name. And then, basically, what kind of behavior you’re looking for each of the elements, but in, like, C or something, it would be the method name and then types of the arguments in order.
Yeah, it basically is all the stuff that makes the function go. You know, you know, function takes in certain parameters it takes in certain, uh, certain values and things like that that are important. It also involves the return of it, right? If I’ve written a function that’s designed to return, you know, a Boolean, you know who are false, and I change it to instead return Uh, an integer I mean, that’s that breaks people’s code that breaks other things. And so the signature is, you know, inclusive of, you know, your parameters, your returns.
Um, if you go look at, like the mdn docs, they’ll tell you it should also, you know, it includes exceptions that get thrown basically any of that sort of interaction surface of your function, anything that people will rely on or use information from. Um so being able to define stuff as private is useful because it helps you remember, You know what’s safe, what isn’t. And what can other people interface with. Ultimately, this is all just making javascript more object oriented.
So one of my favorite things we’re talking about method signatures in PHP. There’s the string positioning method str pos And then there’s also string replace str underscore, replace in string string positioning the argument. The signature is first argument is the haystack. Lykken arguments the needle third arguments the optional offset. But in string replace. First argument is the search. Second argument is the place. Third argument is the haystack. So they completely reverse it.
And this has been the signature for it for pretty much forever. Um, and I can never remember the sequence of those ever. I would always have to look it up. Um, that’s a pain in the ass, but, like, they can’t change it, right? Like you have to come up with a totally new method called, like, better, stronger place or something.
It’s kind of like a fingerprint. You know, if I’ve changed my fingerprint, I’m not me. You know, if I go over to Aaron’s house and and murder his eyeballs and I left fingerprints all over that knife and then I changed my fingerprints, and they can’t prove it was me. If you came
over and then you left and then change your fingerprints and came back, I wouldn’t know who you were.
Yeah, I would be a totally different human being at that point.
I know people strictly through their fingerprint.
Um, two more. Two more things. Um, the first one is weak references and finalize Urz, these go together. Um and these are really aimed at in? In my opinion, like the big app developers, you’ve got a huge dynamic Web app. You’re doing a lot of embedded programming. The example. I was, uh
this is some weird stuff. This is when Michael and I were doing pre show. We’re looking at this, and I was like, This is really weird concern to be worried about for, like, a website.
But what I said was, yeah, it is. That’s exactly right. You would never worry about this with the website, but if you’re writing note applications to put on a raspberry pi nano, well, you’re going to be a little more concerned with the performance of your code in your application because that device doesn’t have that much ram on it. It doesn’t have a lot of CPU power. You this this gets us back to this idea of and I I think about this a lot.
How you know how concerned we used to have to be when we were writing code back in the nineties, You know? And you know, even before that, even in the nineties it was, like world different than in the eighties. You know, when people worry about how much compute cycle you had, how much ram you had access to
back when? When, Uh, yeah. So if you’re if you’re a newer developer and by that I mean, you’ve just gotten development in the last, uh, 10, 15 years, and you haven’t explored a lot of older stuff. Um, the memory where you would store your data and the memory where you would store your program would be the same, like, finite array of memory. And it was very small, and you had to optimize the shit out of your program. So, like, I know that there’s that quote from Bill Gates, like 64 k.
Memory ought to be enough for anyone. But like, that was a lot at the time. And before that, it was like, significantly less. I forget how much less, But the memory registers were like, really small.
It goes all the way down, you know, started with almost nothing. Um, so that’s what this so like, a a week raft is a weak reference. What does a weak reference sound like to you?
Uh, when I here that what I first think of is like doing C or C Plus. Plus, we have pointers, and strong reference is object in memory that has, like, there’s like a pointer is pointing to it actively. And when a pointer stops pointing to it, then it’s a There’s like the original object is weekly reference, they think, or I’m probably getting this wrong, but I haven’t had to deal with the shit.
I’m asking some pretty academic computer science questions here. I know
what the and the end result. The important part is that week weekly referenced objects get picked up by garbage collection before strongly referenced objects.
And that’s the key. Word is the garbage collection, right? So, up until now, basically javascript didn’t have this. You know, everything was strongly referenced and garbage collection just happened as best as it could. Really, Unless you abstracted stuff to a separate cache layer now, you can you can actually create weak references such that when you call something with the D ref method, Then it flags that as being the referenced, literally like you got it.
I think of it like a one time scratch pad. Right. Um, what’s the old cryptographic approach where, like you had your one sheet that you used. And then once you’ve encoded a message with that, you tore that sheet off and crumpled it up and threw away
one time pad.
Yeah, one time pad. It’s kind of like that. You create your object with weak references and reference it the direct method. And once your code sees that, you have referenced it and you called that it says cool. He’s done with this now and the next time garbage collection happens, it knows I can just get rid of that. Um, this is highly dependent on your environment. JavaScript DOESn’t define garbage collection. Browsers define garbage collection, so it’s gonna be different in every environment.
For the most part, that’s you know, that’s just a That’s the computer cleaning things up at that point. So it’s a little special case at that point, but, um, it’s designed to help you maximize your memory efficiency and keep stuff out of the way so that you’re only maintaining references to the stuff you need again important in, you know, low power type environments. Ah, the final Isar goes along with this, it kind of pairs with it. And so what a finalize er does is it lets you create a register where you can assign an object to it.
It’s basically an event listener, specific to garbage collection
and the magic. The magic class. Here is the event registered, one
that’s a finalize. A registry is so finalize already had the class. So you create an object instance of that and reference your object and pass it the information you want to send to it. What does is when the event happens when garbage collection happens and it says, Oh, this objects referenced. It’s in the register. I’m gonna call my function for my finalize a registry and pass that value to it, or this is really useful.
This isn’t a great tool for a production environment, but it can be really great in a development environment if you want to track cash evictions if you want to track performance. If you’re debugging code that you’re getting a lot of like undefined object references and you can’t figure out why, by using that for console logging or, you know, logging to another system or something like that, you can have it tell you. Oh, Hey, Yeah.
Oh, yeah, That’s undefined, because we, you know, we cleaned up that object, you know, 40 seconds ago. And here’s why we did it. Here’s the thing that called that. So you can build logging around your garbage collection to kind of see that very useful for development. Say, not something you’re probably gonna turn on for production environment, but
that’s pretty. I mean, I can see that you’re useful for debugging or like reflection purposes. That’s pretty cool.
Yeah, this is something like weak references and finalize Ear’s are things that you are going to use if you’re doing like some pretty gnarly programming. You will not worried about this for a website,
you know, it’s the other way you would use that to I can imagine is, um, if you were doing some kind of like multitasking thing, like if you had, like, the equivalent of running multiple programs at once on your phone and then if you need to free up the memory from any of the things you can have them registered, and then when you have to put him to sleep, you throw it into serialize er, and you can, you know, freeze them into a serialized plan.
The last one that is big is is my favorite one, because it’s the hardest one. And it’s some new logical assignment operators. So we’ve got we’ve had stuff like the concatenation operator plus equals So X Plus equals one. And if you say let’s let’s back it up you say, you know, let X equal 10 and then you say ex plus equals one then actually equals 11, right? It’s just designed to say Hey, because
because plus equals is X equals X plus whatever.
It’s the shorthand, right? It’s a shorthand way of reducing the amount of code. So okay with you, we’ve got three new ones. Okay, we’ve got a logical and equals, which, when I say logical and that’s in and and and percent 10% percent percent, you have a logical or which is pipe pipe equals and you have a null ish, which is question mark. Question mark equals what? What the hell do these things mean?
One of them. Okay, so in Ruby, we have similar operators. If you put um, like we have or equals, there’s plus equals, slash equals and anything within equals on the end is if you have a and then that and then be it means like, uh, like slash equals means a equals a slash b, um, or the or equals is a equals A or B. So what? What does that mean for javascript?
So yeah. So this is all about assigning a value to something based on some logical assessment of its value. Okay, so let’s take the truth. E the truth. The assignment operator, which is amp. Amp equals
all right. So when I saw this, what I thought is like and I believe this actually exists in Ruby. What that would be is, like a equal. If you had a and equals B, it would be a equals A and B. So basically, it would be, um if you have to be doing strictly with truth and false e values, that would be like if a was true and B was false, then they would be false. But if a was true and B was true, then they would be true,
right? So Truth e. And it’s important to kind of define this right? Truth e is any variable that set. It has a value of some kind false e is an assortment of values so false e could be a literal false. The word false. It could be a zero. A boolean zero is false. E um, null is false. E undefined is false. E not a number in a n is false. E truly is. Is it true? Is it an empty array? You know, Is it an integer? Is it a number? Is it? You know, a string of some kind is in a number.
Um, those are all true truth e um, there’s the third is null ish. No polish is a false e value that is specifically null or undefined. Only so a square is a rectangle. Rectangle isn’t always a square kind of situation with false e and and knowledge. Um, so in the case of truth e, let’s say you have let X equals one. Let y equals two and you say X and and equals y. What is the value of X?
I say it one more time X equals. I’ve had two glasses of tea wings now.
Hey, you knew ahead of time. There’s going to be a math test at the end of this, so it’s not fair I forgot to say that you were warned. Let X equal one. Let y equal to okay, X and and equals y
Okay, um, excess set already. So it should stay as X.
Mm. No x x is set X is one X is true. The since X is true. The senate to why that would be too they
need This needs a better name. Truth is,
it is what it is. So false is the opposite of that. Yeah. So, in the same situation, if X or or equals lie X is one which isn’t false e. So as a result, it would stay as one. It would not get reset to to because it would only be set to two. It was blank, for instance.
Right? Okay, So in in ruby or equals would be used or equals when you have, it’s like when you have a variable and all that you care about is the variable has a value and you would use or equals to basically give it a If you don’t know I have a value. Here is the value for you. Um, so like X. So you have if you have x one and y is two x or equals y Excellent. Equal Wednesday.
Um, the the shorthand stuff gets gnarly, like across the board. Um, here’s let me give you a good example of, like why this matters. Because I the first thing you’re probably gonna say, or anybody’s gonna say Looking at this is why do we need this? I can literally do this with an if statement. Uh, but here’s an example, right. If I say X or or equals y x logical or equals y that’s five bites Mm X pipe pipe equal, wide.
Oh, you don’t have. You don’t have to sell me on that. I I mean, I used this kind of stuff and really all
the time I’m trying to explain to everybody else Oh, I figured they liked it. And I want them to understand. I want I want to I want to I want to show off my knowledge of
So
So we’ve got a five bites statement, right? The equivalent if statement, if you do it all on one line without spaces. If parentheses X is equal to null or X is equal to undefined. Those parentheses. Bracket X equals Y semi colon bracket 31 bites. So code that takes 31 bites. I can do with one of these logical assignment operators in five bites. So you’ve reduced the code size by a factor of five. So, yes, it does the exact same thing. The performance gain is probably minimal, but you do save a lot of space, and it’s wide
I. I like it because idiomatic lee it. The intention reads cleaner like when you have, like whenever I when I’m doing Ruby and I see like a or equals B, I know instantly what that means. I know what the intent of the of the programmer who wrote that initially me or not, was there. They just they want to force. They want to make sure that a has its like safe. It’s like a safe assignment. They want to make sure that a has a value, and that’s going to default to be,
um, you wanna really stupid metaphor? Sure. Why do stenographers use a shorthand typing when we have typewriters?
Um, because they have to type really fast. It’s an
efficiency thing, right? Like you’re like, Yeah, obviously we could just use a normal typewriter and write everything out longhand, but they use a shorthand to be faster and more efficient. But we’re doing so. I
I could write five characters in roughly the same amount of time I could write the 30 characters. I think it wouldn’t be much more. I I think that for me, at least. And and this is as a nerd version person. I think that looking at even in the JavaScript version X and and equals Y if I were to have an intuitive understanding of that operator better than I do right now, I would see that. And I would understand that better than seeing, like, the long hand version of, like, his external.
Or is it because I actually have to read that, have to read that full thing and evaluated in my mind and then figure out Okay, what am I modelling out here? But if I just see like, oh, x question mark question mark equals Why
God, You know what? Where this he’s in for me to that I’ve been doing a lot more of over the last couple of years is using things like turn Eri Operators don’t know Ruby. Have something along those lines.
Yeah, it’s the same one that’s the same.
So a turn every operator is just a shorthand way of setting, like the value of a variable based on a condition. Again, the exact same thing you would do with an if else, basically turn area is an if else. But you say it in one line, and the nice part about it is you can literally say, you know, let X equal and then have your turn every operator, instead of doing like a let X And then, if something that X equal to this else set X equal to this, you can set X equal to your turn every operator and get the value. You know, straight out of that, it’s shorthand.
It’s efficient. Yeah, you don’t have to read through in one line. You can literally do exactly what you need instead of, you know, a minimum of six. Yeah, so that’s the way I look at these. It’s it’s a little gnarly. I get it, Um, there will be a lot of articles in the show notes that go over all of these and have code examples of all of them. I think that’s a really good way especially if you’re If you want to look into the week ref weak references and finalize Ear’s and the logical assignment operators.
Those code examples will help you immensely understand You know what those are doing and how they work. Um, some of this is available in like the latest version of node. Not all of it is again. This is not out yet. It doesn’t release until June. So this is just your time to learn it and figure out what you need and let us know how you’re going to use it in your code.
Mm, That was interesting. Uh, yeah, I know, it’s It’s I I find that I learned the most about things by looking at edges of where they are and are not what they are. And so it’s fun. Like I can get a better understanding of these concepts and jobs are by, like, looking at them compared to other things. Um, like, I’m saying that the similarities and differences and stuff, um, you should, uh, I’m tell us thing on the social. I don’t have anything better than that right now.
Uh, Facebook and twitter dot com slash drunken u x and instagram dot com slash drunken ux podcast and also on drunken ux dot com slash discord.
Should just write you a script.
Have a script, you know, you make it easy.
No, I
like I like I like the challenge trying to remember all the things
if you want to challenge getting in here and reading up on some of these deals and figuring out like numeric separators. Super easy. Uh, understanding logical assignment operators. Not nearly as easy. There are some of that stuff, man. I’m my job script skills. Probably last three years have really, really sharpened a lot. And I’m getting a lot of opportunity to work on. You know, I’m re factoring a ton of my code right now for our form A P. I s, um I’ve committed to making sure all of our functions have Js doc references on them so I can actually generate real documentation on it.
And between the challenges that poses and trying to, you know, make sure I’m not pulling on the threads on sweaters and unraveling like my entire code base. Um, I really find the hardest part about it is keeping your personas close, but your users closer. Bye.
Oh, you motherfucker, totally caught me on that one.
This episode of The Drunken UX Podcast brought to you by nuCloud.