Javascript

Property Descriptors

Writable

The writable attribute within a property descriptor in JavaScript is a fundamental control for property mutability. When writable is set to true, the value of the property can be changed. In contrast, setting writable to false renders the property immutable—any attempts to modify the property will fail.

This failure manifests differently depending on the JavaScript environment's strictness. In strict mode, attempting to change a non-writable property's value will throw a TypeError, halting execution and signaling an error. However, in non-strict (or sloppy) mode, such an attempt will not throw an error; instead, it fails silently, with the script continuing to run without any indication of the failed property assignment. 

Furthermore, the writable attribute's behavior is influenced by the configurable attribute of the property descriptor. A configurable property is one whose descriptor can be changed and the property itself deleted. Notably, if configurable is set to false, the writable attribute can still transition from true to false, but not the other way around. This ensures that a property can become immutable but once made non-writable, it cannot be made writable again if configurable is false.

When a property is defined as non-writable and non-configurable ({ writable: false, configurable: false }), it is akin to a constant on the object; it is shielded from both reassignment and descriptor alterations. Conversely, a fully dynamic property ({ writable: true, configurable: true }) allows for complete freedom in property value and descriptor modifications.

The ability to toggle writable provides developers with a nuanced approach to property management. It allows a property to be mutable during certain phases, such as initialization, and then locked to prevent further changes, a mechanism that is crucial for maintaining data integrity and security in the application.

State and Permission Diagram 

Certainly, adding a `Permit to Change Configurable` column will complete the picture by indicating whether the `configurable` attribute can be changed. Since once a property is set to `configurable: false`, it cannot be changed back to `true`, this column will always have "No" for any state where `configurable` is `false`. Here's the updated truth table:


| Configurable | Enumerable | Writable | Permit to Write | Permit to Enumerate | Permit to Change Writable | Permit to Change Enumerable | Permit to Change Configurable |

|--------------|------------|----------|-----------------|----------------------|---------------------------|-----------------------------|-------------------------------|

| True         | True       | True     | Yes             | Yes                  | Yes                       | Yes                         | Yes                           |

| True         | True       | False    | No              | Yes                  | Yes                       | Yes                         | Yes                           |

| True         | False      | True     | Yes             | No                   | Yes                       | Yes                         | Yes                           |

| True         | False      | False    | No              | No                   | Yes                       | Yes                         | Yes                           |

| False        | True       | True     | Yes             | Yes                  | No                        | No                          | No                            |

| False        | True       | False    | No              | Yes                  | No                        | No                          | No                            |

| False        | False      | True     | Yes             | No                   | No                        | No                          | No                            |

| False        | False      | False    | No              | No                   | No                        | No                          | No                            |


This table now provides a comprehensive view of the permissions available for each possible state of an object's property with respect to the `configurable`, `enumerable`, and `writable` attributes. The final column, `Permit to Change Configurable`, underscores the one-way nature of making a property non-configurable: once set to `false`, it cannot be reverted to `true`.