Objects and Property Descriptors
What an object literal creates under the hood — the writable/enumerable/ configurable flags every property carries, getters/setters, and `Object.freeze`/`seal`.
Objects and Property Descriptors
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `{ key: value }` is shorthand for something more detailed: every property secretly carries a descriptor — `value`, `writable`, `enumerable`, and `configurable` — that you just don't see unless you ask for it with `Object.getOwnPropertyDescriptor`.
- Properties written as object literals default to `writable: true, enumerable: true, configurable: true`; properties defined with `Object.defineProperty` default every flag you don't specify to `false`, which is the opposite default and a common source of "why can't I see this property" bugs.
- A getter or setter replaces a property's `value`/`writable` pair with `get`/`set` functions, so reading or writing the property actually runs your code — useful for validation, computed values, or logging a read, without changing how callers use the property.
- `Object.seal` stops properties from being added or removed but still lets existing writable ones change; `Object.freeze` goes further and makes every existing property read-only too — both are shallow, so a frozen object's nested object is still fully mutable.
- These tools exist to express intent in code, not to add real security — any of them can be undone or worked around by code that controls the object directly, so they protect against accidents, not attackers.
The object literal is hiding detail from you
Write { name: "Ada" } and it feels like the simplest thing in the
language — a label, a colon, a value. But ask JavaScript what’s really
sitting behind that name property and you get more than just the string
"Ada". Every property on every object carries a small bundle of metadata
alongside its value: can this be overwritten (writable), does it show up
when you loop over the object or call Object.keys() (enumerable), and
can its definition be changed or deleted later (configurable). You never
see this bundle when you write object literals because JavaScript quietly
sets all three to true and moves on. The moment you need a property that
behaves differently — read-only, hidden from iteration, locked against
redefinition — you’re working with property descriptors directly, and the
defaults flip on you in a way that trips up almost everyone the first
time.
The three flags, and the default trap
You inspect a property’s descriptor with
Object.getOwnPropertyDescriptor(obj, 'key'), and you set one explicitly
with Object.defineProperty(obj, 'key', { ...descriptor }). The trap is
that these two paths into the same object don’t share defaults. An object
literal’s properties come out writable, enumerable, and configurable
— all permissive. Object.defineProperty, by contrast, defaults every
flag you don’t mention to false — all restrictive. Write
Object.defineProperty(obj, 'id', { value: 42 }) expecting a normal
property, and you’ve actually created one that can’t be overwritten,
can’t be seen by Object.keys(), and can’t be redefined later. None of
that throws an error to warn you; the property just quietly behaves like
nothing you’ve worked with before. This is exactly the kind of
easy-to-miss detail the video works through directly, flag by flag, with
the actual descriptor objects printed out so the defaults stop being
invisible.
Getters and setters: a property that runs code
A property doesn’t have to just hold a value — it can hold a pair of
functions instead, a get and a set, and from the outside it still
looks and behaves like a plain property. person.fullName can read like
a normal field while actually running firstName + ' ' + lastName behind
the scenes every time it’s accessed. Assigning to person.age = -5 can
run a set function that validates the input and throws or clamps the
value instead of silently storing a number that doesn’t make sense. The
point of a getter/setter pair is that callers never need to know — they
keep writing obj.prop and obj.prop = x exactly as before, while you
get to run arbitrary code on either side of that interaction.
Sealing and freezing: locking the door, not the house
Object.seal(obj) stops properties from being added or removed, but any
property that was already writable stays writable — you can still
change existing values, just not the object’s shape. Object.freeze(obj)
takes that further: every existing property also becomes non-writable, so
nothing about the object can change at all. Both operations are shallow.
Freezing { settings: { theme: 'dark' } } locks settings itself from
being reassigned to a different object, but the object currently sitting
in settings is completely untouched — obj.settings.theme = 'light'
still works fine. This catches people who assume “frozen” means “frozen
all the way down.”
It’s worth being honest about what these tools are for. They’re not a
security boundary — any code running in the same process can read an
object before it’s frozen, or just hold an unfrozen reference to the same
data. What freeze and seal actually buy you is a way to say, in code,
“this shouldn’t change” and have the language enforce that against
accidental mutation — your own bugs, not someone else’s.
Where this fits
This is the layer underneath object literals, and it’s the layer the next
two lessons build directly on. Prototypal inheritance and Object.create
uses Object.defineProperty to explain exactly how a property lands on an
object versus on something further up its prototype chain, and class
syntax later in this group is, under the hood, just a more convenient way
of calling the same descriptor machinery you just saw directly. Knowing
what a property really is — value plus three flags, or getter/setter
instead of value — makes both of those lessons mechanics instead of magic.
Check yourself
If you write `const obj = { a: 1 }`, what are the `writable`, `enumerable`, and `configurable` flags on property `a`?
All three default to `true` for a property created through an object literal. You can overwrite `a`, see it in a `for...in` loop or `Object.keys()`, and delete or redefine it.
You add a property with `Object.defineProperty(obj, 'x', { value: 5 })` and then try `obj.x = 10`. What happens, and why?
The assignment silently fails (or throws in strict mode) and `obj.x` is still `5`. `Object.defineProperty` defaults every flag you don't specify to `false`, so leaving out `writable` makes the property read-only — the opposite of what an object literal would have given you.
What's the practical difference between a data property and an accessor property?
A data property stores a `value` directly. An accessor property stores `get` and/or `set` functions instead — reading it calls `get`, writing it calls `set`. From the outside, `obj.prop` looks identical either way; the difference is entirely in what happens underneath when you touch it.
After calling `Object.freeze(obj)`, can you still change `obj.nested.count` if `nested` is an object?
Yes. `Object.freeze` only locks the properties directly on `obj` — it's shallow. `obj.nested` itself can't be reassigned to a different object, but the object it already points to is untouched by the freeze and can still be mutated freely.
Why doesn't `Object.freeze` count as real security against a determined caller?
Because anything running in the same JavaScript environment as the object has just as much access to the language as you did when you froze it — it can read the object before you freeze it, hold a separate reference that was never frozen, or simply ignore a silent failed write. `Object.freeze` communicates intent and catches accidents; it doesn't create a boundary the way a server-side permission check does.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.