Progress pill
Intermediate JavaScript

Working with Objects

  • Private Properties
  • Static Properties
  • Get and set
In this chapter, we'll learn some powerful and slightly more advanced tools for working with objects in JavaScript.

Private Properties

Sometimes, we want to hide a property of an object so that it can't be changed or accessed from outside the object. JavaScript gives us a way to do this using # before the property name. This creates a private property, which is only accessible from inside the class.
class Person { #age // this is a private property constructor(name, age) { this.name = name this.#age = age } getAge() { return this.#age } } const alice = new Person("Alice", 30) console.log(alice.name) // Alice console.log(alice.getAge()) // 30, the method can access the private property console.log(alice.#age) // ❌ Error! You can't access private properties directly
Private properties are helpful when you want to prevent accidental changes.

static Properties

Sometimes, you want a property to belong to the class itself, not to each object you create from that class. That's what static is for. A static property is contained in the class and all of the objects of that class will refer to it.
class User { static counter = 0 // this belongs to the class, not to instances. The same counter will be shared by all objects constructor() { User.counter++ // changes the static property every time an object of this class gets initiated } } const a = new User() // the constructor will change the shared counter from 0 to 1 const b = new User() // the constructor will change the shared counter from 1 to 2 console.log(User.counter) // prints 2
This is useful for for storing shared data and methods that applies to the whole group of objects, not just one.

get and set

In JavaScript, get and set let you make properties that look like normal variables, but actually run special code in the background.
A getter method runs when you try to read a property. It is declared by writing get before a method with the name of the property.
class User { constructor(firstName, lastName) { this.firstName = firstName this.lastName = lastName } get fullName() { return `${this.firstName} ${this.lastName}` } } const user = new User("Jane", "Doe") console.log(user.fullName) // Jane Doe
Even though fullName is not a regular property, we can use it like one, and behind the scenes it runs the get function to build the full name.
A setter method runs when you assign a value to a property. It lets you control what happens when someone tries to change that value. It is declared by writing set before a method with the name of the property.
class User { constructor() { this.firstName = "John" this.lastName = "Doe" } get fullName() { return `${this.firstName} ${this.lastName}` } set fullName(input) { // gets the name that is passed const parts = input.split(" ") // breaks it into parts this.firstName = parts[0] // uses the first part as first name this.lastName = parts[1] // uses the second part as last name } } const user = new User() user.fullName = "John Smith" console.log(user.firstName) // John console.log(user.lastName) // Smith
When we do user.fullName = "John Smith", it runs the set method and updates the firstName and lastName values.
So even though it feels like we’re just setting a simple variable, we’re actually triggering logic that updates other properties.