Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Freezing the Object You Mean

Freezing the Object You Mean

In this lesson, you lock Harbour Prints’ settings with Object.freeze() and check exactly which objects the lock reaches. The proof card export reads the shop’s settings: its name, and a nested limits record with a cap of 20 copies.

In Python, the settings sit in a MappingProxyType, a read-only view of a dictionary. A stray line raises the nested copy limit to 25, past the cap, and the read-only settings let it through: the script prints 25. The proxy refuses writes to its own keys, but the limits dictionary is a separate object that it only points to.

In the JavaScript port, const stops a name from being reassigned. Object.freeze() locks the object you pass it: nothing can be added to it, and its own properties can’t be removed or changed.

Watch out: freezing isn’t deep. It locks only the properties config owns, not the objects they point to, so the same nested write follows the reference to copyLimits and changes it. JavaScript logs 25, as Python did.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll lock Harbour Prints’ settings with Object.freeze(), and check which objects the lock reaches. The proof card export reads the shop’s settings: a name, and nested limits capped at 20 copies.

00:14 In Python, they sit in a MappingProxyType, a read-only view of a dictionary. A stray line sets the nested limit to 25, past the cap, and the read-only settings let it through: the script prints 25.

00:30 The proxy refuses writes to its own keys, but the limits dictionary is a separate object.

00:38 In the JavaScript port, const stops a name from being reassigned. Object.freeze() locks the object you pass it: nothing can be added to it, and its own properties can’t be removed or changed.

00:51 Watch out: freezing isn’t deep. It locks only the properties config owns, not the objects they point to. So the nested write follows the arrow to the limits object and changes it.

01:06 Run it, and JavaScript logs 25, as Python did.

01:11 To see which object the lock reached, ask Object.isFrozen(). It returns true for an object that’s frozen. For config, it logs true.

01:24 For copyLimits, it logs false. Freezing config left the object behind its arrow open. And copyLimits is declared with const, yet its object still changed: const guards the name, not the object.

01:41 Object.freeze() also hands back the object it froze, so you can freeze and name an object in one line. Freezing config again does no harm, and triple equals compares the result with config.

01:54 It logs true: the same object. Here the Python analogy stops. MappingProxyType wraps the dictionary in a new view, and the dictionary underneath stays writable through any other name for it.

02:10 Object.freeze() makes no view: it changes the object itself, so every name for it sees the lock.

02:18 Now change the shop name, a property config owns. try and catch work like Python’s try and except, but catch names no type: it catches every error.

02:30 So the block logs its name, which holds the type, as type(err).__name__ does. The write throws, and the catch block logs TypeError.

02:40 In Python, the same write to the proxy raises a TypeError too. In JavaScript, the refusal comes from the property’s flags. Freezing set writable and configurable to false on each of config’s data properties.

02:56 The copy limit shouldn’t change either. So freeze the limits object explicitly, where you create it. In Python, the nested dictionary gets its own proxy. Each object you mean to lock needs its own call, because freezing never follows references for you.

03:15 JavaScript now tries the nested write, then logs the limit.

03:23 Python wraps its write in try and except the same way. Run them. Both objects now report frozen. The nested write throws a TypeError, as Python raises one, and the limit stays at 20.

03:39 Remember the order: total is a getter that adds up its quantities list, and extra is a setter that adds one more. Once the customer approves the order, freeze it, and Object.isFrozen() reports true.

03:55 Then assign 4 to extra: does it throw a TypeError, like the shop name did? Your turn: pause the video and predict.

04:08 No error: order.total logs 9.

04:13 An accessor has no value to lock: freezing only made extra non-configurable, so its setter still ran and added 4 to the list. The list is another object, and nothing froze it.

04:27 So freeze the object you mean. Object.freeze() locks one object’s own properties, and hands that same object back. Freeze each nested object you know about explicitly, and check with Object.isFrozen().

04:43 And const guards only the name. Next up is the text lesson Choosing a Fixed Key Set. There, you’ll compare freezing with sealing and with stopping additions, by what each one changes: new properties, and the flags on existing ones.

Become a Member to join the conversation.