JavaScript Private Class Fields
New Course Coming Soon:
Get Really Good at Git
Introduction and code samples on using private class fields in JavaScript.
Before the introduction of private class fields, we could not really enforce private properties on a class. We used conventions instead, maybe using _
as an hint that the field is private, like this:
class Counter {
_count = 0
increment() {
this._count++
}
}
But we could access the count using
const counter = new Counter()
counter._count
We can now use private class fields that enforce private fields:
class Counter {
#count = 0
increment() {
this.#count++
}
}
We now can’t access this value from the outside. Trying to access it will raise a syntax error.
This is part of the new class fields proposal, which you can use since Chrome 72 and Node 12.
Are you intimidated by Git? Can’t figure out merge vs
rebase? Are you afraid of screwing up something any time
you have to do something in Git? Do you rely on ChatGPT
or random people’s answer on StackOverflow to fix your
problems? Your coworkers are tired of explaining Git to
you all the time? Git is something we all need to use,
but few of us really master it. I created this course to
improve your Git (and GitHub) knowledge at a radical
level. A course that helps you feel less frustrated with
Git. Launching Summer 2024. Join the waiting list!
→ Get my
JavaScript Beginner's Handbook
→ Read my
JavaScript Tutorials
on The Valley of Code
→ Read my
TypeScript Tutorial
on The Valley of Code
Here is how can I help you:
- COURSES where I teach everything I know
- THE VALLEY OF CODE your web development manual
- BOOTCAMP 2024 cohort in progress, next edition in 2025
- BOOKS 16 coding ebooks you can download for free on JS Python C PHP and lots more
- SOLO LAB everything I know about running a lifestyle business as a solopreneur
- Interesting links collection
- Follow me on X