Professional JS: Features You Need to Know

Static Fields & Initialization Blocks

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Professional JS: Features You Need to Know

Check out a free preview of the full Professional JS: Features You Need to Know course

The "Static Fields & Initialization Blocks" Lesson is part of the full, Professional JS: Features You Need to Know course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates how to define static fields and methods. He also introduces the concept of an initialization block, which is a block of code that is executed only once when the class is being parsed.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Static Fields & Initialization Blocks" Lesson

[00:00:00]
>> Maximiliano Firtman: Okay, so next is about static fields and methods, that it's actually not really so important because it wasn't actually so complicated to do it before this. So when you say a static field? So if I have a class for example, let's say I have a person or that and I say well, I want to define a property that applies to all the persons.

[00:00:23]
For example, I can say person maxAge. Let's say 150, let's be optimistic about this, so 150. In this case, it's a property that is applied to all the persons. And the way to do that was to create a dynamic property like this before, but it's actually outside of the body, it's not within the body declaration.

[00:00:48]
So now the difference that we can say static, maxAge, and we can put a value here, that's the difference. So now we define that within the body, which sounds and the same happens with methods. So we can have a method that applies, I don't know, like list all, that will do something.

[00:01:10]
Well, now this applies to not one person, so if I have here, John, John.lIstAll is wrong, right? Because it's not an instance method, it's person.listAll because it's an a static method like in Java or other languages where you do have static. Okay, make sense? The thing from ES2022.

[00:01:40]
That is kind of really, really new. Have in mind that ES2022 was the spec was finalized June 2022. That doesn't mean that browsers started to implement that, that at that precise moment, they take some months and versions. So maybe at the end of this was actually implemented in most of the browsers.

[00:02:07]
Okay, makes sense? And the next one I actually, I can continue here, it's actually also new in chats script, it's called initialization block. So actually you saved static, you open a code block and you write code. You say what? What is that? What is that for? It's actually called, it's like a constructor for the static class and not for instances, but it's going to be executed once.

[00:02:42]
Okay, it's a singleton execution. So everything you put here cons law, I am in the static initialization block that's going to be executed only once in the lifetime of your app when the class is being passed, okay? Make sense? Without that, it's something that you put outside so or after.

[00:03:10]
So it's just code that you put after defining the class, but now it's within the body, so it's semantically attached to the creation of that class.
>> Maximiliano Firtman: Okay, make sense? It's just that. It's not a big deal, but it's actually something that needs to be processed by our minds to get used to that, I need to find use cases for that as well.

[00:03:37]
Okay, so sometimes now, when you create classes and you're using OOP, some code that you have just in the body of your module, that code, that is not in a function. It's just like global code that you have there, now you can put that within the static block that will make more semantic sense.

[00:03:59]
That's the idea, okay? Such that they are so that well these code belongs to this class.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now