Object Methods & Property Descriptors

Unlock the hidden configuration of objects. Learn how to iterate over keys, freeze data, and tweak invisible property flags.

JavaScript7 min readConcept 21 of 62

The Object Utility Methods

Because objects aren't arrays, you can't just map() or filter() them directly. Instead, JavaScript provides static methods on the global Object class to extract their data into arrays.

Object.keys(obj) returns an array of the keys.

Object.values(obj) returns an array of the values.

Object.entries(obj) returns an array of [key, value] arrays, perfect for looping.

Freezing and Sealing

Sometimes you need to lock down an object to prevent other code from modifying it.

Object.freeze(obj) makes an object completely immutable. You cannot add, delete, or change any properties.

Object.seal(obj) is slightly less strict. You cannot add or delete properties, but you *can* still change the values of existing properties.

Property Descriptors

Under the hood, every property has hidden configuration flags called 'descriptors'. You can view them using Object.getOwnPropertyDescriptor(obj, 'key').

The flags are: writable (can the value change?), enumerable (will it show up in loops/keys?), and configurable (can it be deleted or have its flags changed?).

Descriptor Anatomy

Analyze how Object methods extract data, and see the hidden descriptor flags that power properties.

Descriptor Anatomy

// 1. The Target Object
const user = {
role: "admin",
level: 5
};
// 2. Object Extraction Methods
Object.keys(user);
["role", "level"]
Object.values(user);
["admin", 5]
Object.entries(user);
[["role", "admin"],
["level", 5]]
// 3. Property Descriptors & Freezing
Object.freeze(user);
Under the Hood: 'role' Property
Property: role
value"admin"
writable
false
enumerable
true
configurable
false
❄️
iWhen we run Object.freeze(user), JavaScript quietly changes the hidden writable and configurable descriptors to false, locking the property forever.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1Which method should you use if you want to loop through both the keys and values of an object?
  2. 2What is the difference between Object.freeze() and Object.seal()?
  3. 3What does the `enumerable` descriptor flag do?

Remember this

  • Object.keys(), values(), and entries() are the standard ways to extract data from objects into arrays.
  • Object.freeze() makes an object completely immutable (shallowly).
  • Object.seal() prevents adding/deleting keys, but allows updating existing values.
  • Property Descriptors hide flags like writable, enumerable, and configurable behind every property.

Done with this concept?

Mark it complete to track your progress. No login needed.