Sub topic 3
Objects as References: Shallow and Deep Copies
This chapter assumes you can create objects, read and write their properties, and use this in methods, as covered in the earlier chapters. It explains what happens when objects are copied, inherited from, described, and locked.
Objects as References
A variable does not hold the object itself. It holds a reference to the object. Assigning that variable to another variable copies the reference, so both variables point to the same object. Spread syntax ({ ...obj }) makes a shallow copy: it creates a new top-level object, but any nested objects inside it are still shared with the original. structuredClone makes a deep copy, which shares nothing with the original.
const original = { name: "Ada", tags: ["math"] };
const shallow = { ...original };
shallow.tags.push("code");
console.log(original.tags); // ["math", "code"]
const deep = structuredClone(original);
deep.tags.push("poetry");
console.log(original.tags); // ["math", "code"]The Prototype Chain
When you read a property and the object does not have it as an own property, JavaScript looks it up on the object's prototype, then on that prototype's prototype, and so on until it reaches null. Object.create(proto) makes a new object whose prototype is proto. Writing a property always creates or updates an own property and leaves the prototype unchanged. The exception is a setter on the prototype: assigning to that property calls the setter instead.
Property Descriptors
Every property has attributes. writable controls whether the value can be changed. enumerable controls whether the property shows up in Object.keys and for...in. configurable controls whether the property can be deleted or have its attributes redefined. If you add a property with Object.defineProperty and leave an attribute out, that attribute defaults to false.
Getters and Setters
An accessor property runs a function when it is read (a getter) or assigned (a setter). It does not store a value itself.
const temp = {
celsius: 20,
get fahrenheit() { return this.celsius * 9 / 5 + 32; },
set fahrenheit(f) { this.celsius = (f - 32) * 5 / 9; }
};
temp.fahrenheit = 212;
console.log(temp.celsius); // 100
Object.defineProperty(temp, "id", { value: 1 });
console.log(Object.getOwnPropertyDescriptor(temp, "id"));
// { value: 1, writable: false, enumerable: false, configurable: false }
console.log(Object.keys(temp)); // ["celsius", "fahrenheit"]Locking Objects
| Method | Add props | Delete props | Change values |
|---|---|---|---|
| `Object.preventExtensions` | No | Yes | Yes |
| `Object.seal` | No | No | Yes |
| `Object.freeze` | No | No | No |
All three methods are shallow: they affect only the object's own properties, not the objects it references. In sloppy mode, a blocked change fails silently. In strict mode, it throws a TypeError.
const config = Object.freeze({ port: 8080, db: { host: "localhost" } });
config.db.host = "example.com"; // allowed: freeze is shallow
console.log(config.db.host); // "example.com"
console.log(Object.isFrozen(config.db)); // falseExercise
You run const a = { list: [1] }; const b = { ...a }; Object.freeze(b); b.list.push(2);. What is a.list, and why?
Show answer
[1, 2]. Spread copies the reference to the array, so a and b share it. Object.freeze is shallow and freezes only b's own properties, so the shared array can still be changed.