Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Classes  ›  Lesson

Getters, Setters and Static Members

Static Members 12 minget/set define properties that run code on read or write while still looking like plain property access; static fields and methods belong to the class itself, shared by all instances, and are never accessible through an instance.

Getters, Setters and Static Members

In short: get name() { ... } and set name(value) { ... } define a property that looks like plain data (obj.name, no parentheses) but actually runs code whenever it's read or written — useful for computed values or validation. static fields and methods work completely differently: they belong to the class itself, shared by every instance, and they are never copied onto instances the way regular methods and constructor-set properties are. Reach for a static member for anything that's true of the whole class — a running count, a factory method — not any one particular object.

A flat with a get/set minutes dial: this._seconds = 210 stored, s.minutes reads 3.5, s.minutes = 4 writes _seconds = 240; next to a building holding instance a and instance b, with a shared noticeboard on the OUTSIDE reading static count — ONE shared value — Song.count/Song.describeAll() belong to the class itself, and a.describeAll() fails because static methods live on the class, never on an instance

Read and write a computed property

A getter and setter pair lets song.minutes behave like ordinary data, while secretly converting to and from a different stored value:

javascript

s.minutes reads 3.5 — no parentheses, just like a plain property, even though a whole calculation runs behind it. s.minutes = 4 looks like an ordinary assignment too, but it actually calls the set minutes method, which converts 4 minutes back into 240 stored seconds.

Track something true of the whole class, not one instance

A static field belongs to the class — every constructor call can update the same shared value, and a static method is only callable on the class itself:

javascript

Song.count is one shared number, incremented by every new Song(...) call — not a separate counter per instance. Song.describeAll() is only reachable on the class itself.

The trap: reaching for a static method through an instance

A static member is never copied onto instances — reaching for it through one fails, even though the class it belongs to made that very instance:

javascript

This throws TypeError: s.describeAll is not a function. describeAll genuinely doesn't exist on s — only on Song itself. Static members and instance members live in two entirely separate places.

Quick practice

quick drill

What does get minutes() { return this._seconds / 60; } let you write?

quick drill

What does s.minutes = 4; actually do, given a set minutes(value) method?

quick drill

Where does a static field or method live?

quick drill

What happens when you call a static method through an instance, like s.describeAll()?

quick drill

Given static count = 0; and every constructor doing Song.count++, does each instance get its own separate count?

🎯 Your turn

Given class Song { static count = 0; constructor(title) { this.title = title; Song.count++; } static describeAll() { return \${Song.count} songs`; } }, explain why const s = new Song("a"); s.describeAll();throws, whileSong.describeAll()works, and whySong.count` correctly reflects every instance ever created.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainClass-level, not instance-level

Given class Song { static count = 0; constructor(title) { this.title = title; Song.count++; } static describeAll() { return ${Song.count} songs; } }, explain why const s = new Song("a"); s.describeAll(); throws, while Song.describeAll() works, and why Song.count correctly reflects every instance ever created.