JavaScript Read-Only property


Example

5

Using property descriptors we can make a property read only, and any attempt to change it's value will fail silently, the value will not be changed and no error will be thrown.

The writable property in a property descriptor indicates whether that property can be changed or not.

var a  = { };

Object.defineProperty(a, 'foo', { value: 'original', writable: false });

a.foo = 'new';

console.log(a.foo);

Console output

original